Skip to content

DISC: add accessor attributes to Index for consistency with Series #17134

Open
@jbrockmendel

Description

@jbrockmendel

Broken off of #17117 for discussion, xref #8162, #17061, recent mailing list thread.

With a datetime-like column we can access year, hour, ... with self.dt.foo. With a DatetimeIndex (PeriodIndex, ...) we access these attributes directly self.foo without the .dt. I'd like to add a .dt property to the appropriate Index subclasses so that these attributes can be accessed symmetrically. i.e. instead of:

if isinstance(obj, pd.Index):
    year = obj.year
elif isinstance(obj, pd.Series):
    year = obj.dt.year

we can just use year = obj.dt.year regardless.

The implementation is three lines in core.indexes.datetimelike.DatetimeIndexOpsMixin`:

    @property
    def dt(self):
        return self

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions