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 whicheverobjhas, so code can calltz_localizeand 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.dtifobjhas it, elseobj.
See also
convert_timezonesUses 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'