Skip to content
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

BUG: Timestamp.floor() method not able to handle DST change in local time #44287

Closed
3 tasks done
m-kremer opened this issue Nov 2, 2021 · 8 comments
Closed
3 tasks done
Labels
Bug Needs Info Clarification about behavior needed to assess issue Timezones Timezone data dtype Usage Question

Comments

@m-kremer
Copy link

m-kremer commented Nov 2, 2021

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the master branch of pandas.

Reproducible Example

import pandas as pd

# Example 1: Timestamp('2021-10-31 02:30:00+0200', tz='Europe/Amsterdam')
timestamp = (pd.to_datetime('2021-10-31 00:30:00')
             .tz_localize('UTC')
             .tz_convert('Europe/Amsterdam'))

ts_hour = timestamp.floor('60T')

# Returns: AmbiguousTimeError
# Expected: Timestamp('2021-10-31 02:00:00+0200', tz='Europe/Amsterdam')

# Example 2: Timestamp('2021-10-31 02:30:00+0100', tz='Europe/Amsterdam')
timestamp = (pd.to_datetime('2021-10-31 01:30:00')
             .tz_localize('UTC')
             .tz_convert('Europe/Amsterdam'))

ts_hour = timestamp.floor('60T')

# Returns: AmbiguousTimeError
# Expected: Timestamp('2021-10-31 02:00:00+0100', tz='Europe/Amsterdam')

Issue Description

The timestamp is in local time, so pandas should how enough information to know how to round it down to the whole hour. However, it does not recognize this and asks for an ambiguous argument.

Adding ambiguous=True does not provide the desired result.

Expected Behavior

See reproducible example

Installed Versions

INSTALLED VERSIONS

commit : 945c9ed
python : 3.9.2.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19041
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 12, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : Dutch_Netherlands.1252

pandas : 1.3.4
numpy : 1.21.3
pytz : 2021.3
dateutil : 2.8.2
pip : 21.3.1
setuptools : 58.4.0
Cython : None
pytest : 6.2.4
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.8.6 (dt dec pq3 ext lo64)
jinja2 : 2.11.3
IPython : 7.29.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.4.2
numexpr : None
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.7.1
sqlalchemy : 1.4.13
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@m-kremer m-kremer added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 2, 2021
@mroeschke
Copy link
Member

It appears the ambiguous keyword will give you the correct result?

In [12]: timestamp = (pd.to_datetime('2021-10-31 00:30:00')
    ...:              .tz_localize('UTC')
    ...:              .tz_convert('Europe/Amsterdam'))
    ...:
    ...: ts_hour = timestamp.floor('H', ambiguous=True)

In [13]: ts_hour
Out[13]: Timestamp('2021-10-31 02:00:00+0200', tz='Europe/Amsterdam')

In [14]: timestamp = (pd.to_datetime('2021-10-31 01:30:00')
    ...:              .tz_localize('UTC')
    ...:              .tz_convert('Europe/Amsterdam'))
    ...:
    ...: ts_hour = timestamp.floor('60T', ambiguous=False)

In [15]: ts_hour
Out[15]: Timestamp('2021-10-31 02:00:00+0100', tz='Europe/Amsterdam')

@mroeschke mroeschke added Needs Info Clarification about behavior needed to assess issue Timezones Timezone data dtype and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 6, 2021
@m-kremer
Copy link
Author

m-kremer commented Nov 7, 2021

Your example is producing the desired result, but the argument should not be needed in this case; there is no ambiguity here.

And its quite a hassle to manually check if time is in DST or not when working with a large timeseries dataset...

@mroeschke
Copy link
Member

The flooring is done in local time, so 02:30:00 floor 60T = 2:00:00. Since this is on 2021-10-31, it's ambiguous whether this is +0200 or +0100.

@m-kremer
Copy link
Author

m-kremer commented Nov 7, 2021

The timestamp includes timezone information, so I would expect that to be taken into account, but I understand its not. Is that something that could potentially be improved?

@jreback
Copy link
Contributor

jreback commented Nov 7, 2021

@m-kremer once u floor the time it's not clear which fold it's in and the result could be 2am or 1am; it's a user decision here

@m-kremer
Copy link
Author

m-kremer commented Nov 7, 2021

That's not true if you would take tz info into account:

02:30:00+0200 floor 60T = 02:00:00+0200

02:30:00+0100 floor 60T = 02:00:00+0100

No ambiguity here...

@jreback
Copy link
Contributor

jreback commented Nov 7, 2021

@m-kremer and sure for a subset of operations this may actually work. but as a general rule, e.g. arithmetic (+/-) and rounding operations you inevitably get to an ambiguous state because working with a timezone in an operation has so many edge cases. The general rule is easy to understand & test (operate in local non-tz aware time)

@m-kremer
Copy link
Author

m-kremer commented Nov 8, 2021

Thanks @jreback, now I better understand why its working this way.

This issue can be closed... :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Info Clarification about behavior needed to assess issue Timezones Timezone data dtype Usage Question
Projects
None yet
Development

No branches or pull requests

3 participants