In Pandas datetime can be two types:

  • naive - no time zone information
    • Timestamp('2022-08-24 16:53:23.186691')
  • aware - time zone information and local time
    • Timestamp('2022-08-24 15:53:23.595457+0200', tz='Europe/Rome')
Pro Tip 1
Always work with timezone information when possible. It's recommended to work with a single time zone - usually it's UTC.

Below we can find several Pandas examples of timestamps naive and aware.

Naive (local time)

The first example shows how to get the current date and time as a naive timestamp in Pandas.

pd.Timestamp.now()

result:

Timestamp('2022-08-24 16:53:23.186691')

Timezone aware (UTC)

Get UTC time as timezone aware timestamp in Pandas:

pd.Timestamp.utcnow()

result:

Timestamp('2022-08-24 13:53:23.387282+0000', tz='UTC')

Naive (local time)

We can get the local time of any time zone as naive timestamp by:

pd.Timestamp.now(tz='Europe/Rome').tz_localize(None)

result:

Timestamp('2022-08-24 15:53:23.783935')

Timezone aware (local time)

To use the time aware local time in Pandas:

pd.Timestamp.now(tz='Europe/Rome')

result:

Timestamp('2022-08-24 15:53:23.595457+0200', tz='Europe/Rome')   

Naive (UTC)

There are multiple ways to get naive UTC time. Below we will describe some of them.

tz_localize

The method pandas.DataFrame.tz_localize will localize the tz-naive index of a Series or DataFrame to target the time zone.

pd.Timestamp.utcnow().tz_localize(None)  

result:

Timestamp('2022-08-24 13:53:24.147123')    

tz_convert

Method pandas.DataFrame.tz_convert convert the tz-aware axis to the target time zone.

pd.Timestamp.utcnow().tz_convert(None)

result:

Timestamp('2022-08-24 13:53:24.377683')
Pro Tip 1
Difference between
tz_convert
and
tz_localize
is that first removes the timezone information resulting in naive local time while second remove the timezone information but converting to UTC, so giving naive UTC time

tz_convert and .now(tz='Europe/Rome')

One more example for tz_convert(None):

pd.Timestamp.now(tz='Europe/Rome').tz_convert(None)

result:

Timestamp('2022-08-24 13:53:23.983624')

Pandas to_datetime - naive or aware

Pandas to_datetime method has parameter: utc:

  • True - the function always returns a timezone-aware UTC-localized Timestamp, Series or DatetimeIndex.
    • To do this, timezone-naive inputs are localized as UTC, while timezone-aware inputs are converted to UTC.
  • False - inputs will not be coerced to UTC.
    • Timezone-naive inputs will remain naive, while timezone-aware ones will keep their time offsets. Limitations exist for mixed offsets (typically, daylight savings), see Examples section for details.

More examples and explanation can be found here: pandas.to_datetime