-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: DatetimeIndex.unique shifts tz-aware dates #21737
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
Thanks, I can confirm that this issue is occurring on master. A simpler demonstration of the bug: In [2]: pd.__version__
Out[2]: '0.24.0.dev0+219.g1070976'
In [3]: dti = pd.DatetimeIndex(['2017', '2017'], tz='US/Eastern')
In [4]: dti
Out[4]: DatetimeIndex(['2017-01-01 00:00:00-05:00', '2017-01-01 00:00:00-05:00'], dtype='datetime64[ns, US/Eastern]', freq=None)
In [5]: dti.unique()
Out[5]: DatetimeIndex(['2017-01-01 05:00:00-05:00'], dtype='datetime64[ns, US/Eastern]', freq=None) Surprisingly, we actually have a test for this, but it doesn't look to be working as intended: pandas/pandas/tests/indexes/datetimes/test_datetime.py Lines 378 to 385 in 1070976
Manually inspecting the test shows that the comparing the indexes themselves returns In [6]: result = dti.unique()
In [7]: result
Out[7]: DatetimeIndex(['2017-01-01 05:00:00-05:00'], dtype='datetime64[ns, US/Eastern]', freq=None)
In [8]: expected = pd.DatetimeIndex(['2017'], tz='US/Eastern')
In [9]: expected
Out[9]: DatetimeIndex(['2017-01-01 00:00:00-05:00'], dtype='datetime64[ns, US/Eastern]', freq=None)
In [10]: result == expected
Out[10]: array([ True])
In [11]: result[0] == expected[0]
Out[11]: False Notice that In [12]: result.values
Out[12]: array(['2017-01-01T05:00:00.000000000'], dtype='datetime64[ns]')
In [13]: expected.values
Out[13]: array(['2017-01-01T05:00:00.000000000'], dtype='datetime64[ns]') This has something to do with what In [14]: result2 = pd.DatetimeIndex([result[0]])
In [15]: result2
Out[15]: DatetimeIndex(['2017-01-01 05:00:00-05:00'], dtype='datetime64[ns, US/Eastern]', freq=None)
In [16]: expected2 = pd.DatetimeIndex([expected[0]])
In [17]: expected2
Out[17]: DatetimeIndex(['2017-01-01 00:00:00-05:00'], dtype='datetime64[ns, US/Eastern]', freq=None)
In [18]: result2 == expected2
Out[18]: array([False])
In [19]: result2.values
Out[19]: array(['2017-01-01T10:00:00.000000000'], dtype='datetime64[ns]')
In [20]: expected2.values
Out[20]: array(['2017-01-01T05:00:00.000000000'], dtype='datetime64[ns]') Finally, note that In [21]: dti
Out[21]: DatetimeIndex(['2017-01-01 00:00:00-05:00', '2017-01-01 00:00:00-05:00'], dtype='datetime64[ns, US/Eastern]', freq=None)
In [22]: pd.unique(dti)
Out[22]: DatetimeIndex(['2017-01-01 00:00:00-05:00'], dtype='datetime64[ns, US/Eastern]', freq=None) |
I faced the same bug yesterday.
However, to reproduce it in Jupyter Notebook (just being curious about it), the bug is shown even different!!
And that is
:
|
So there's some funniness when passing a
So there might be a bug that lies deeper when setting a But adding the
|
So this appears fixed on master: (maybe due to #20912)
Although we already have a test for |
Thanks everyone for your work tracking this down and getting a fix in place quickly. |
Code Sample, a copy-pastable example if possible
Problem description
The normalize method correctly sets the DatetimeIndex to midnight but the unique method returns a new DatetimeIndex not set to midnight, rather it returns a DatetimeIndex time as if converted to UTC (07:00 in the example code) but still retains the correct timezone ('America/Los_Angeles' in the example code). The code returns:
DatetimeIndex(['2018-07-03 07:00:00-07:00', '2018-07-04 07:00:00-07:00'], dtype='datetime64[ns, America/Los_Angeles]', freq=None)
Expected Output
DatetimeIndex(['2018-07-03 00:00:00-07:00', '2018-07-04 00:00:00-07:00'], dtype='datetime64[ns, America/Los_Angeles]', freq=None)
Output of
pd.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Linux
OS-release: 4.15.0-24-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_CA.UTF-8
LOCALE: en_CA.UTF-8
pandas: 0.23.1
pytest: None
pip: 10.0.1
setuptools: 39.2.0
Cython: 0.28.3
numpy: 1.14.3
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 6.4.0
sphinx: None
patsy: None
dateutil: 2.7.3
pytz: 2018.3
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 1.0.1
sqlalchemy: 1.2.7
pymysql: None
psycopg2: 2.7.4 (dt dec pq3 ext lo64)
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: 0.4.1
pandas_datareader: None
The text was updated successfully, but these errors were encountered: