lair.clock.datetime_accessor#

lair.clock.datetime_accessor(obj, accessor='dt')[source]#

Return the datetime accessor of the object.

A Series of times has its datetime methods under .dt; a DatetimeIndex has them itself. This returns whichever obj has, so code can call tz_localize and the rest on either.

Parameters:
  • obj (pd.Series | pd.DatetimeIndex | Any) – The times.

  • accessor (str, optional) – The name of the accessor, by default 'dt'.

Returns:

Any – obj.dt if obj has it, else obj.

See also

convert_timezones

Uses it for a Series or an index.

Examples

>>> import pandas as pd
>>> from lair.clock import datetime_accessor
>>> times = pd.Series(pd.date_range("2024-01-01", periods=2, freq="h"))
>>> type(datetime_accessor(times)).__name__
'DatetimeProperties'
>>> type(datetime_accessor(pd.DatetimeIndex(times))).__name__
'DatetimeIndex'