-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DOC: add an example to PeriodIndex.to_timestamp
to clarify the behavior in case len(index)<3
#57384
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
DOC: add an example to PeriodIndex.to_timestamp
to clarify the behavior in case len(index)<3
#57384
Conversation
@MarcoGorelli, I added an example to clarify the behavior of |
pandas/core/arrays/period.py
Outdated
@@ -636,6 +636,12 @@ def to_timestamp(self, freq=None, how: str = "start") -> DatetimeArray: | |||
>>> idx.to_timestamp() | |||
DatetimeIndex(['2023-01-01', '2023-02-01', '2023-03-01'], | |||
dtype='datetime64[ns]', freq='MS') | |||
|
|||
We can not infer the frequency if len(index) < 3: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No big deal, but I'd personally phrase this as The frequency will not be inferred if the index contains less than three elements:
. I had to read the current version couple of times to understand it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the comment @datapythonista, I corrected docstring as you suggested. I also added a new example to emphasize importance values of index to be strictly monotonic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!
pandas/core/arrays/period.py
Outdated
The frequency will not be inferred if the index contains less than | ||
three elements and values of index are not strictly monotonic: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the way this is phrased it sounds like (pseudocode):
if len(index) < 3 and not index.is_monotonic():
dont_infer_frequency(...)
Instead, I think it's more like
if len(index) < 3 or not index.is_monotonic():
dont_infer_frequency(...)
?
How about
The frequency will not be inferred if the index contains less than
three elements, or if the values of index are not strictly monotonic:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, you are right, my wording isn't correct. I changed it as you suggested.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks @natmokval
…vior in case len(index)<3 (pandas-dev#57384) * add an example to PeriodIndex.to_timestamp * correct to_timestamp docs, add an example * correct the wording in to_timestamp docstring
xref #56213
added an example to
PeriodIndex.to_timestamp
to clarify the behavior in caselen(index) < 3