Skip to content

implement .dt accessor for Index classes #18429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

jbrockmendel
Copy link
Member

index.dt --> self for DatetimeIndex, TimedeltaIndex, PeriodIndex. Raises AttributeError for others (identical error to Series.dt).

@jorisvandenbossche in the discussion in #17134 you had a concern about tab completion/namespacing. Is that a sticking point for you?

@codecov
Copy link

codecov bot commented Nov 22, 2017

Codecov Report

Merging #18429 into master will decrease coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #18429      +/-   ##
==========================================
- Coverage   91.35%   91.33%   -0.02%     
==========================================
  Files         163      163              
  Lines       49714    49719       +5     
==========================================
- Hits        45415    45410       -5     
- Misses       4299     4309      +10
Flag Coverage Δ
#multiple 89.13% <100%> (-0.01%) ⬇️
#single 39.64% <60%> (-0.06%) ⬇️
Impacted Files Coverage Δ
pandas/core/indexes/datetimelike.py 97.13% <100%> (+0.01%) ⬆️
pandas/core/indexes/base.py 96.43% <100%> (ø) ⬆️
pandas/io/gbq.py 25% <0%> (-58.34%) ⬇️
pandas/io/json/table_schema.py 94.44% <0%> (-1.39%) ⬇️
pandas/core/frame.py 97.8% <0%> (-0.1%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d421a09...bda766e. Read the comment docs.

@codecov
Copy link

codecov bot commented Nov 22, 2017

Codecov Report

Merging #18429 into master will decrease coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #18429      +/-   ##
==========================================
- Coverage   91.35%   91.33%   -0.02%     
==========================================
  Files         163      163              
  Lines       49714    49719       +5     
==========================================
- Hits        45415    45410       -5     
- Misses       4299     4309      +10
Flag Coverage Δ
#multiple 89.13% <100%> (-0.01%) ⬇️
#single 39.64% <60%> (-0.06%) ⬇️
Impacted Files Coverage Δ
pandas/core/indexes/base.py 96.43% <100%> (ø) ⬆️
pandas/core/indexes/datetimelike.py 97.13% <100%> (+0.01%) ⬆️
pandas/io/gbq.py 25% <0%> (-58.34%) ⬇️
pandas/io/json/table_schema.py 94.44% <0%> (-1.39%) ⬇️
pandas/core/frame.py 97.8% <0%> (-0.1%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d421a09...bda766e. Read the comment docs.

@jorisvandenbossche
Copy link
Member

@jorisvandenbossche in the discussion in #17134 you had a concern about tab completion/namespacing. Is that a sticking point for you?

Yes, I think so (and not only for tab completion). Doing df.index.dt.mean() should be invalid IMO.

@jbrockmendel
Copy link
Member Author

Yes, I think so (and not only for tab completion). Doing df.index.dt.mean() should be invalid IMO.

OK. Would returning self.to_series().dt solve your concerns? @jreback back in the issue you were pretty adamant that returning anything other than self would be inconsistent. Can you two reach a consensus on this?

@jorisvandenbossche
Copy link
Member

Would returning self.to_series().dt solve your concerns?

No, because that would return a Series, while the Index.dt. attributes should return an Index IMO (see #17134 (comment), which @jreback agrees with).
So it's more an implementation question: it should be like returning self, but a restricted version.

Eg we could add a switch to _delegate_method to return either raw index result or wrapped in Series ?

@jbrockmendel
Copy link
Member Author

No, because that would return a Series, while the Index.dt. attributes should return an Index IMO

I had hoped to avoid introducing a new class, but that should be doable. There is a lot of machinery in indexes.datetimelike, some of which can probably be made useful.

@jreback
Copy link
Contributor

jreback commented Nov 22, 2017

how is this any more complicated than for .str?
str = accessor.AccessorProperty(strings.StringMethods)

@jreback jreback added API Design Compat pandas objects compatability with Numpy or Python functions labels Nov 22, 2017
@jorisvandenbossche jorisvandenbossche removed the Compat pandas objects compatability with Numpy or Python functions label Nov 22, 2017
@jbrockmendel
Copy link
Member Author

how is this any more complicated than for .str?
str = accessor.AccessorProperty(strings.StringMethods)

StringMethods has special handling for cases where it is derived from a Series vs Index. I can do something analogous in indexes.accessors, but last time I tried that you said it was unnecessarily complicated and should just return self (in fairness, that implementation was unnecessarily complicated and returned a Series instead of an Index...)

So let's nail down the specs before I implement anything else. It modifying indexes.accessors to behave analogously to StringMethods going to make everyone happy?

@jreback
Copy link
Contributor

jreback commented Nov 23, 2017

So let's nail down the specs before I implement anything else. It modifying indexes.accessors to behave analogously to StringMethods going to make everyone happy?

I think these should all be consistent, so yes, not sure there should actually be that much complexity.

@jreback
Copy link
Contributor

jreback commented Dec 28, 2017

closing as stale, if you want to work on this, pls ping.

I think the idea of this PR is fine, but needs resolution

@jreback jreback closed this Dec 28, 2017
@jbrockmendel jbrockmendel deleted the index_dt_accessor2 branch February 11, 2018 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DISC: add accessor attributes to Index for consistency with Series
3 participants