'how to create a certain pandas data type (date and time with timezone)

I want to use datetime.datetime.now() to create the time in the following format to match the output from an API: 2022-02-25T18:05:00+00:00.

When I use dtypes on the API output it says datetime64[ns, UTC]. I'm not sure what that means.



Solution 1:[1]

Using dt.strftime, this is as close as you get:

>>> df["column"] = df["column"].dt.strftime("%Y-%m-%dT%H:%M:%S%z")
>>> df
                     column
0  2022-02-25T20:25:16+0000

But you can go all the way operating on the string

>>> df["column"] = df["column"].str.slice(stop=-2) + ":" + df["column"].str.slice(start=-2)
>>> df
                      column
0  2022-02-25T20:25:16+00:00

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 aaossa