I have multiple data frames with a datetime column as a string. Datetime formats vary across columns in the dataframe or across dataframes. I want to get a unix timestamp that gets interpreted by an ArcGIS application into the local timezone.
For example, one such dataframe is the following:
import pandas as pd
time_dict = {"datetime": ["2023-08-15T15:32:47.687+00:00", ""]}
test_df = pd.DataFrame(time_dict)
I tried a number of simple functions. When I submitted the unix timestamp to the ArcGIS application, it gave a date-time that was 4 hours earlier than the local time. When I tried to make some corrections for the timezone, I encountered a TypeError saying that the timezone was already present in the date.
I will provide a solution below, but maybe someone has a better one.
datetime.now()
, converting to unix time, pushing to the application, and then seeing a very different time from the one on my computer.datetime.now().timestamp()
, Python will assume that you want local date/time, so to calculate Unix time, it will convert that to UTC internally, then calculate Unix time. pandas on the other hand, using e.g.pd.TImestamp("now").timestamp()
will give you something that looks like local, but behaves like UTC (compare the timestamps...). So, some footguns available.