Skip to content

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

Merged

Conversation

natmokval
Copy link
Contributor

@natmokval natmokval commented Feb 12, 2024

xref #56213

added an example to PeriodIndex.to_timestamp to clarify the behavior in case len(index) < 3

@natmokval natmokval assigned natmokval and unassigned natmokval Feb 12, 2024
@natmokval natmokval added the Docs label Feb 12, 2024
@natmokval natmokval marked this pull request as ready for review February 12, 2024 22:27
@natmokval
Copy link
Contributor Author

@MarcoGorelli, I added an example to clarify the behavior of PeriodIndex.to_timestamp in case len(index) < 3. Could you please take a look at this PR?

@@ -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:
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

Comment on lines 640 to 641
The frequency will not be inferred if the index contains less than
three elements and values of index are not strictly monotonic:
Copy link
Member

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:

Copy link
Contributor Author

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.

Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @natmokval

@MarcoGorelli MarcoGorelli added this to the 3.0 milestone Feb 15, 2024
@MarcoGorelli MarcoGorelli merged commit e5a0df9 into pandas-dev:main Feb 15, 2024
pmhatre1 pushed a commit to pmhatre1/pandas-pmhatre1 that referenced this pull request May 7, 2024
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants