-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Fix incorrect DTI/TDI indexing; warn before dropping tzinfo #22549
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
Changes from 8 commits
dd5cf53
4d34e14
d9cb515
5d2782a
a52af41
0c5e652
e9c3dd4
77abd10
acf7b6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -246,15 +246,19 @@ def setup_method(self, method): | |
def test_to_period_millisecond(self): | ||
index = self.index | ||
|
||
period = index.to_period(freq='L') | ||
with tm.assert_produces_warning(UserWarning): | ||
# warning that timezone info will be lost | ||
period = index.to_period(freq='L') | ||
assert 2 == len(period) | ||
assert period[0] == Period('2007-01-01 10:11:12.123Z', 'L') | ||
assert period[1] == Period('2007-01-01 10:11:13.789Z', 'L') | ||
|
||
def test_to_period_microsecond(self): | ||
index = self.index | ||
|
||
period = index.to_period(freq='U') | ||
with tm.assert_produces_warning(UserWarning): | ||
# warning that timezone info will be lost | ||
period = index.to_period(freq='U') | ||
assert 2 == len(period) | ||
assert period[0] == Period('2007-01-01 10:11:12.123456Z', 'U') | ||
assert period[1] == Period('2007-01-01 10:11:13.789123Z', 'U') | ||
|
@@ -266,81 +270,95 @@ def test_to_period_tz_pytz(self): | |
|
||
ts = date_range('1/1/2000', '4/1/2000', tz='US/Eastern') | ||
|
||
result = ts.to_period()[0] | ||
expected = ts[0].to_period() | ||
with tm.assert_produces_warning(UserWarning): | ||
# warning that timezone info will be lost | ||
result = ts.to_period()[0] | ||
expected = ts[0].to_period() | ||
|
||
assert result == expected | ||
tm.assert_index_equal(ts.to_period(), xp) | ||
assert result == expected | ||
tm.assert_index_equal(ts.to_period(), xp) | ||
|
||
ts = date_range('1/1/2000', '4/1/2000', tz=UTC) | ||
ts = date_range('1/1/2000', '4/1/2000', tz=UTC) | ||
|
||
result = ts.to_period()[0] | ||
expected = ts[0].to_period() | ||
result = ts.to_period()[0] | ||
expected = ts[0].to_period() | ||
|
||
assert result == expected | ||
tm.assert_index_equal(ts.to_period(), xp) | ||
assert result == expected | ||
tm.assert_index_equal(ts.to_period(), xp) | ||
|
||
ts = date_range('1/1/2000', '4/1/2000', tz=tzlocal()) | ||
ts = date_range('1/1/2000', '4/1/2000', tz=tzlocal()) | ||
|
||
result = ts.to_period()[0] | ||
expected = ts[0].to_period() | ||
result = ts.to_period()[0] | ||
expected = ts[0].to_period() | ||
|
||
assert result == expected | ||
tm.assert_index_equal(ts.to_period(), xp) | ||
assert result == expected | ||
tm.assert_index_equal(ts.to_period(), xp) | ||
|
||
def test_to_period_tz_warning(self): | ||
# GH#21333 make sure a warning is issued when timezone | ||
# info is lost | ||
dti = date_range('1/1/2000', '4/1/2000', tz='US/Eastern') | ||
with tm.assert_produces_warning(UserWarning): | ||
# warning that timezone info will be lost | ||
dti.to_period() | ||
|
||
def test_to_period_tz_explicit_pytz(self): | ||
xp = date_range('1/1/2000', '4/1/2000').to_period() | ||
|
||
ts = date_range('1/1/2000', '4/1/2000', tz=pytz.timezone('US/Eastern')) | ||
|
||
result = ts.to_period()[0] | ||
expected = ts[0].to_period() | ||
with tm.assert_produces_warning(UserWarning): | ||
# warning that timezone info will be lost | ||
result = ts.to_period()[0] | ||
expected = ts[0].to_period() | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in future PR these should be parameterized |
||
assert result == expected | ||
tm.assert_index_equal(ts.to_period(), xp) | ||
assert result == expected | ||
tm.assert_index_equal(ts.to_period(), xp) | ||
|
||
ts = date_range('1/1/2000', '4/1/2000', tz=pytz.utc) | ||
ts = date_range('1/1/2000', '4/1/2000', tz=pytz.utc) | ||
|
||
result = ts.to_period()[0] | ||
expected = ts[0].to_period() | ||
result = ts.to_period()[0] | ||
expected = ts[0].to_period() | ||
|
||
assert result == expected | ||
tm.assert_index_equal(ts.to_period(), xp) | ||
assert result == expected | ||
tm.assert_index_equal(ts.to_period(), xp) | ||
|
||
ts = date_range('1/1/2000', '4/1/2000', tz=tzlocal()) | ||
ts = date_range('1/1/2000', '4/1/2000', tz=tzlocal()) | ||
|
||
result = ts.to_period()[0] | ||
expected = ts[0].to_period() | ||
result = ts.to_period()[0] | ||
expected = ts[0].to_period() | ||
|
||
assert result == expected | ||
tm.assert_index_equal(ts.to_period(), xp) | ||
assert result == expected | ||
tm.assert_index_equal(ts.to_period(), xp) | ||
|
||
def test_to_period_tz_dateutil(self): | ||
xp = date_range('1/1/2000', '4/1/2000').to_period() | ||
|
||
ts = date_range('1/1/2000', '4/1/2000', tz='dateutil/US/Eastern') | ||
|
||
result = ts.to_period()[0] | ||
expected = ts[0].to_period() | ||
with tm.assert_produces_warning(UserWarning): | ||
# warning that timezone info will be lost | ||
result = ts.to_period()[0] | ||
expected = ts[0].to_period() | ||
|
||
assert result == expected | ||
tm.assert_index_equal(ts.to_period(), xp) | ||
assert result == expected | ||
tm.assert_index_equal(ts.to_period(), xp) | ||
|
||
ts = date_range('1/1/2000', '4/1/2000', tz=dateutil.tz.tzutc()) | ||
ts = date_range('1/1/2000', '4/1/2000', tz=dateutil.tz.tzutc()) | ||
|
||
result = ts.to_period()[0] | ||
expected = ts[0].to_period() | ||
result = ts.to_period()[0] | ||
expected = ts[0].to_period() | ||
|
||
assert result == expected | ||
tm.assert_index_equal(ts.to_period(), xp) | ||
assert result == expected | ||
tm.assert_index_equal(ts.to_period(), xp) | ||
|
||
ts = date_range('1/1/2000', '4/1/2000', tz=tzlocal()) | ||
ts = date_range('1/1/2000', '4/1/2000', tz=tzlocal()) | ||
|
||
result = ts.to_period()[0] | ||
expected = ts[0].to_period() | ||
result = ts.to_period()[0] | ||
expected = ts[0].to_period() | ||
|
||
assert result == expected | ||
tm.assert_index_equal(ts.to_period(), xp) | ||
assert result == expected | ||
tm.assert_index_equal(ts.to_period(), xp) | ||
|
||
def test_to_period_nofreq(self): | ||
idx = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-04']) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from datetime import timedelta | ||
from datetime import datetime, timedelta | ||
|
||
import pytest | ||
import numpy as np | ||
|
@@ -41,6 +41,15 @@ def test_getitem(self): | |
tm.assert_index_equal(result, expected) | ||
assert result.freq == expected.freq | ||
|
||
@pytest.mark.parametrize('key', [pd.Timestamp('1970-01-01'), | ||
pd.Timestamp('1970-01-02'), | ||
datetime(1970, 1, 1)]) | ||
def test_timestamp_invalid_key(self, key): | ||
# GH#20464 | ||
tdi = pd.timedelta_range(0, periods=10) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we have a test that indexes with NaT? (both Timedelta and Datetime dtype) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have one for timedelta, just added one for datetime |
||
with pytest.raises(TypeError): | ||
tdi.get_loc(key) | ||
|
||
|
||
class TestWhere(object): | ||
# placeholder for symmetry with DatetimeIndex and PeriodIndex tests | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if we use isna else for NaT checking?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in this context
isna
is less clear. Since there is a specific na-like object we are catching here, we should be explicit about it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok this comment looks good