Skip to content

ENH: Fix total seconds in timedelta #45129

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

deponovo
Copy link
Contributor

@deponovo deponovo commented Dec 30, 2021

As of now, I could not figure out what's leading to test failing in pandas\tests\scalar\timestamp\test_constructors.py:609.

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

needs a test case

@jreback jreback added the Timedelta Timedelta data type label Dec 31, 2021
@jreback
Copy link
Contributor

jreback commented Dec 31, 2021

also some tests are failing

@deponovo
Copy link
Contributor Author

deponovo commented Dec 31, 2021

also some tests are failing

Yes, I referred that in the entry PR message. I could not figure out what the reason is and I am lacking the time to further seek the source. Maybe somebody already knows the issue or could help?
The hour difference from a given timezone flips ahead (but short) of the real timestamp flip to the next hour.

cc @mroeschke

@mroeschke
Copy link
Member

This comment might potentially be relevant https://github.com/pandas-dev/pandas/pull/31155/files#r370721655

@deponovo deponovo force-pushed the fix_total_seconds_in_timedelta branch from db1bf84 to cafd7dd Compare January 3, 2022 11:56
@deponovo deponovo changed the title Fix total seconds in timedelta ENH: Fix total seconds in timedelta Jan 3, 2022
>>> td.total_seconds(ns_precision=True)
521403.010010012
"""
if ns_precision:
Copy link
Contributor

Choose a reason for hiding this comment

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

umm what is this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could not find a way to prevent the problems that the higher precision causes in timezone-related-info (see link from @mroeschke) and because of that I am keeping the super implementation the default. However, having a total_seconds method unable to output nanosecond precision is not consistent. My solution for this was to override the method with this version that has a documented kwarg such that anybody can then know about it (possibly after a bad surprise) without having to check source code.

Copy link
Contributor

Choose a reason for hiding this comment

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

adding a keyword like this is not going to fly. if you want to try to fix that issue then happy to incorporate here (or in a pre-cursor). that looks like the test case needs to be updated.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed. The signatures need to be kept compatible with the datetime.timedelta methods as Timedelta is a subclass

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok. Will retry at a later time.

@deponovo
Copy link
Contributor Author

deponovo commented Jan 9, 2022

@mroeschke Now back to the original method signature:

    def total_seconds(self):
        """Total seconds in the duration."""
        return self.value / 1_000_000_000

This is puzzling. I can't still figure out the source of the prob. I modified the nanosecond variable from _Timestamp in pandas/_libs/tslibs/timestamps.pxd to be public (just for testing purposes) and set it to 0 by default. Then tested a boundary condition:

>>> td = pd.Timestamp(1552211999999999999,  tz="dateutil/America/Los_Angeles")
>>> td
Timestamp('2019-03-10 01:59:59.999999-0800', tz='dateutil/US/Pacific')
>>> td.value
1552211999999999999
>>> td.nanosecond
0
>>> td.nanosecond = 999  # actually just assigning the nanosecond part of the original value
>>> td  # note the false -7
Timestamp('2019-03-10 01:59:59.999999999-0700', tz='dateutil/US/Pacific')
>>> td.value
1552211999999999999
>>> td.nanosecond
999

@mroeschke
Copy link
Member

@mroeschke Now back to the original method signature:

    def total_seconds(self):
        """Total seconds in the duration."""
        return self.value / 1_000_000_000

This is puzzling. I can't still figure out the source of the prob. I modified the nanosecond variable from _Timestamp in pandas/_libs/tslibs/timestamps.pxd to be public (just for testing purposes) and set it to 0 by default. Then tested a boundary condition:

>>> td = pd.Timestamp(1552211999999999999,  tz="dateutil/America/Los_Angeles")
>>> td
Timestamp('2019-03-10 01:59:59.999999-0800', tz='dateutil/US/Pacific')
>>> td.value
1552211999999999999
>>> td.nanosecond
0
>>> td.nanosecond = 999  # actually just assigning the nanosecond part of the original value
>>> td  # note the false -7
Timestamp('2019-03-10 01:59:59.999999999-0700', tz='dateutil/US/Pacific')
>>> td.value
1552211999999999999
>>> td.nanosecond
999

Looking into https://github.com/pandas-dev/pandas/pull/31155/files#r370721655 might solve this issue

@github-actions
Copy link
Contributor

This pull request is stale because it has been open for thirty days with no activity. Please update and respond to this comment if you're still interested in working on this.

@github-actions github-actions bot added the Stale label Feb 12, 2022
@mroeschke
Copy link
Member

Thanks for the pull request but appears to have gone stale. If you're interested in continuing please merge in main, address the comments and we can reopen.

@mroeschke mroeschke closed this Feb 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Stale Timedelta Timedelta data type
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: total_seconds() method returns zero for timedeltas smaller then 1 microsecond
3 participants