-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: when floor
ing, ambiguous
parameter unnecessarily used (and raising Error)
#49565
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
Comments
ambiguous
parameter used but not needed when floor
ingfloor
ing, ambiguous
parameter unnecessarily used (and raising Error)
I can take this one |
I believe the explicitness of the passing |
Agree with @mroeschke , it's fine to error and the error message is clear - closing for now then |
I've dug some deeper, and I'm confused. On the one hand, according to the docs, using
(I don't understand the Going off that, my usage is incorrect and this ticket can be closed. On the other hand, it seems that the documentation is not always correct. Relevant here: in the behavior of I still think the current behavior is not ideal, having to wrap each call in a
Note the in such a situation. In other words, the value of the |
thanks - reopening for now, will take a closer look later |
Ah good spot, thanks for raising this
I don't think your usage is incorrect - rather, it would probably be better if pandas were to raise if anything other than a boolean, 'raise', or 'NaT' is passed to
not sure what you mean here, sorry - could you expand please? |
Thanks for having another look. Here's my comment, sorry it's a bit long. My last comment is no longer valid the way I initially meant it. In the original example, all valid values of However, I also found another unambiguous timestamp, where the value of ts_ambiguous = pd.Timestamp("2020-10-25 02:30", tz='Europe/Berlin') #AmbiguousTimeError
ts_unamb = pd.Timestamp("2020-10-25 02:30+0200", tz='Europe/Berlin') # no error, because not ambiguous So far, so good. As it's clear from the UTC offset that
This is where it's then necessary to catch errors, e.g. like so: import pytz
import pandas as pd
def floor(ts: pd.Timestamp) -> pd.Timestamp:
"""Floor ``ts`` to nearest hour, assuming the Europe/Berlin timezone."""
try:
return ts.floor('H')
except pytz.AmbiguousTimeError:
if ts.utcoffset().total_seconds() == 7200:
return ts.floor('H', ambiguous=True)
else:
return ts.floor('H', ambiguous=False)
floor(pd.Timestamp('2020-10-25 02:30+0200', tz='Europe/Berlin')) # Timestamp('2020-10-25 02:00:00+0200', tz='Europe/Berlin')
floor(pd.Timestamp('2020-10-25 02:30+0100', tz='Europe/Berlin')) # Timestamp('2020-10-25 02:00:00+0100', tz='Europe/Berlin') This error catching probably leaves out edge cases and might return incorrect results in cases where |
I believe the code I pushed before fixes this issue. Before I pushed my code, when I tested Timestamp.floor with something like this: ts.floor('H', ambiguous='hi') , it didn't throw an error and just returned the timestamp as normal (although this was on my local machine so someone should verify this is the actual behavior). Either way, I think that either all invalid arguments to ambiguous should be treated the same, or we add support for 'infer' in Timestamp.floor |
OK let's reopen, I'll leave some comments there |
Well since |
Pandas version checks
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 main branch of pandas.
Reproducible Example
Issue Description
The value of the
ambiguous
parameter is used by the method, even if there is no ambiguity.Expected Behavior
Both calls to the
.floor
method should give the same outcome.Installed Versions
INSTALLED VERSIONS
commit : 91111fd
python : 3.9.13.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 165 Stepping 2, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : de_DE.cp1252
pandas : 1.5.1
numpy : 1.23.4
pytz : 2022.6
dateutil : 2.8.2
setuptools : 63.4.1
pip : 22.2.2
Cython : None
pytest : 7.1.3
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.5.0
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.6.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None
The text was updated successfully, but these errors were encountered: