-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Fix inconsistency in Partial String Index with 'second' resolution #14856
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
Closed
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ea51437
Made this code clearer.
ischurov cc86bdd
Fix inconsistency in Partial String Index with 'second' resolution
ischurov b30039d
Make flake8 happy.
ischurov 9b55117
Addressing code review
ischurov c901588
Addressing code review: testing different combinations with the loop …
ischurov 67e6bab
Addressing code review: more comments added
ischurov e17d210
- Whatsnew section added
ischurov 40eddc3
- Documentation fixes
ischurov c287845
conflict PR #14856 resolved
ischurov d215905
- Addressing code review: documentation clarification.
ischurov 0814e5b
- Addressing code review: added reference to new docs section in what…
ischurov 0e87874
resolved merge conflict in whatsnew/v0.20.0.txt
ischurov ac8758e
resolved merge conflict in whatsnew/v0.20.0.txt
ischurov 2881a53
Merge branch 'datetimeindex-slices' of https://github.com/ischurov/pa…
ischurov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -266,16 +266,15 @@ def test_indexing(self): | |
expected = ts['2013'] | ||
assert_series_equal(expected, ts) | ||
|
||
# GH 3925, indexing with a seconds resolution string / datetime object | ||
# GH14826, indexing with a seconds resolution string / datetime object | ||
df = DataFrame(randn(5, 5), | ||
columns=['open', 'high', 'low', 'close', 'volume'], | ||
index=date_range('2012-01-02 18:01:00', | ||
periods=5, tz='US/Central', freq='s')) | ||
expected = df.loc[[df.index[2]]] | ||
result = df['2012-01-02 18:01:02'] | ||
assert_frame_equal(result, expected) | ||
|
||
# this is a single date, so will raise | ||
self.assertRaises(KeyError, df.__getitem__, '2012-01-02 18:01:02', ) | ||
self.assertRaises(KeyError, df.__getitem__, df.index[2], ) | ||
|
||
def test_recreate_from_data(self): | ||
|
@@ -4897,6 +4896,64 @@ def test_partial_slice_daily(self): | |
|
||
self.assertRaises(Exception, s.__getitem__, '2004-12-31 00') | ||
|
||
# GH14856 | ||
# DatetimeIndex without explicit freq | ||
df = DataFrame({'a': [1, 2, 3]}, DatetimeIndex(['2011-12-31', | ||
'2012-01-01', | ||
'2012-01-02']), | ||
dtype=np.int64) | ||
|
||
self.assertEqual(df.index.resolution, 'day') | ||
|
||
# Timestamp with resolution 'day' | ||
# Should be exact match for series and raise KeyError for Frame | ||
for ts, expected in (('2011-12-31', 1), ('2012-01-01', 2), | ||
('2012-01-02', 3)): | ||
result = df['a'][ts] | ||
self.assertIsInstance(result, np.int64) | ||
self.assertEqual(result, expected) | ||
self.assertRaises(KeyError, df.__getitem__, ts) | ||
|
||
# Timestamp with resolution less precise than 'day' | ||
for ts in ('2011', '2011-12'): | ||
# Series should return slice | ||
result = df['a'][ts] | ||
expected = df['a'][:1] | ||
assert_series_equal(result, expected) | ||
|
||
# Frame should return slice as well | ||
result = df[ts] | ||
expected = df[:1] | ||
assert_frame_equal(result, expected) | ||
|
||
# The same as previous but several elements in the slice | ||
for ts in ('2012', '2012-01'): | ||
# Series should return slice | ||
result = df['a'][ts] | ||
expected = df['a'][1:] | ||
assert_series_equal(result, expected) | ||
|
||
# Frame should return slice as well | ||
result = df[ts] | ||
expected = df[1:] | ||
assert_frame_equal(result, expected) | ||
|
||
# Timestamp with resolution more precise than 'day' | ||
# Compatible with existing key | ||
for ts in ('2012-01-01 00', '2012-01-01 00:00', | ||
'2012-01-01 00:00:00'): | ||
result = df['a'][ts] | ||
self.assertIsInstance(result, np.int64) | ||
self.assertEqual(result, 2) | ||
self.assertRaises(KeyError, df.__getitem__, ts) | ||
|
||
# Timestamp with resolution more precise than 'day' | ||
# Not compatible with existing key | ||
for ts in ('2012-01-01 01', '2012-01-01 00:01', | ||
'2012-01-01 00:00:01'): | ||
self.assertRaises(KeyError, df['a'].__getitem__, ts) | ||
self.assertRaises(KeyError, df.__getitem__, ts) | ||
|
||
def test_partial_slice_hourly(self): | ||
rng = DatetimeIndex(freq='T', start=datetime(2005, 1, 1, 20, 0, 0), | ||
periods=500) | ||
|
@@ -4911,6 +4968,63 @@ def test_partial_slice_hourly(self): | |
self.assertEqual(s['2005-1-1 20:00'], s.ix[0]) | ||
self.assertRaises(Exception, s.__getitem__, '2004-12-31 00:15') | ||
|
||
# GH14856 | ||
# DatetimeIndex without explicit freq | ||
df = DataFrame({'a': [1, 2, 3]}, DatetimeIndex(['2011-12-31 23', | ||
'2012-01-01 00', | ||
'2012-01-01 01']), | ||
dtype=np.int64) | ||
|
||
self.assertEqual(df.index.resolution, 'hour') | ||
|
||
# Timestamp with resolution 'hour' | ||
# Should be exact match for series and raise KeyError for Frame | ||
for ts, expected in (('2011-12-31 23', 1), | ||
('2012-01-01 00', 2), | ||
('2012-01-01 01', 3)): | ||
result = df['a'][ts] | ||
self.assertIsInstance(result, np.int64) | ||
self.assertEqual(result, expected) | ||
self.assertRaises(KeyError, df.__getitem__, ts) | ||
|
||
# Timestamp with resolution less precise than 'hour' | ||
for ts in ('2011', '2011-12', '2011-12-31'): | ||
# Series should return slice | ||
result = df['a'][ts] | ||
expected = df['a'][:1] | ||
assert_series_equal(result, expected) | ||
|
||
# Frame should return slice as well | ||
result = df[ts] | ||
expected = df[:1] | ||
assert_frame_equal(result, expected) | ||
|
||
# The same as previous but several elements in the slice | ||
for ts in ('2012', '2012-01', '2012-01-01'): | ||
# Series should return slice | ||
result = df['a'][ts] | ||
expected = df['a'][1:] | ||
assert_series_equal(result, expected) | ||
|
||
# Frame should return slice as well | ||
result = df[ts] | ||
expected = df[1:] | ||
assert_frame_equal(result, expected) | ||
|
||
# Timestamp with resolution more precise than 'hour' | ||
# Compatible with existing key | ||
for ts in ('2012-01-01 00:00', '2012-01-01 00:00:00'): | ||
result = df['a'][ts] | ||
self.assertIsInstance(result, np.int64) | ||
self.assertEqual(result, 2) | ||
self.assertRaises(KeyError, df.__getitem__, ts) | ||
|
||
# Timestamp with resolution more precise than 'day' | ||
# Not compatible with existing key | ||
for ts in ('2012-01-01 00:01', '2012-01-01 00:00:01'): | ||
self.assertRaises(KeyError, df['a'].__getitem__, ts) | ||
self.assertRaises(KeyError, df.__getitem__, ts) | ||
|
||
def test_partial_slice_minutely(self): | ||
rng = DatetimeIndex(freq='S', start=datetime(2005, 1, 1, 23, 59, 0), | ||
periods=500) | ||
|
@@ -4925,6 +5039,64 @@ def test_partial_slice_minutely(self): | |
self.assertEqual(s[Timestamp('2005-1-1 23:59:00')], s.ix[0]) | ||
self.assertRaises(Exception, s.__getitem__, '2004-12-31 00:00:00') | ||
|
||
# GH14856 | ||
# DatetimeIndex without explicit freq | ||
df = DataFrame({'a': [1, 2, 3]}, | ||
DatetimeIndex(['2011-12-31 23:59', | ||
'2012-01-01 00:00', | ||
'2012-01-01 00:01']), | ||
dtype=np.int64) | ||
|
||
self.assertEqual(df.index.resolution, 'minute') | ||
|
||
# Timestamp with resolution 'minute' | ||
# Should be exact match for series and raise KeyError for Frame | ||
for ts, expected in (('2011-12-31 23:59', 1), | ||
('2012-01-01 00:00', 2), | ||
('2012-01-01 00:01', 3)): | ||
result = df['a'][ts] | ||
self.assertIsInstance(result, np.int64) | ||
self.assertEqual(result, expected) | ||
self.assertRaises(KeyError, df.__getitem__, ts) | ||
|
||
# Timestamp with resolution less precise than 'minute' | ||
for ts in ('2011', '2011-12', '2011-12-31', '2011-12-31 23'): | ||
# Series should return slice | ||
result = df['a'][ts] | ||
expected = df['a'][:1] | ||
assert_series_equal(result, expected) | ||
|
||
# Frame should return slice as well | ||
result = df[ts] | ||
expected = df[:1] | ||
assert_frame_equal(result, expected) | ||
|
||
# The same as previous but several elements in the slice | ||
for ts in ('2012', '2012-01', '2012-01-01', '2012-01-01 00'): | ||
# Series should return slice | ||
result = df['a'][ts] | ||
expected = df['a'][1:] | ||
assert_series_equal(result, expected) | ||
|
||
# Frame should return slice as well | ||
result = df[ts] | ||
expected = df[1:] | ||
assert_frame_equal(result, expected) | ||
|
||
# Timestamp with resolution more precise than 'minute' | ||
# Compatible with existing key | ||
ts = '2012-01-01 00:00:00' | ||
result = df['a'][ts] | ||
self.assertIsInstance(result, np.int64) | ||
self.assertEqual(result, 2) | ||
self.assertRaises(KeyError, df.__getitem__, ts) | ||
|
||
# Timestamp with resolution more precise than 'day' | ||
# Not compatible with existing key | ||
ts = '2012-01-01 00:00:01' | ||
self.assertRaises(KeyError, df['a'].__getitem__, ts) | ||
self.assertRaises(KeyError, df.__getitem__, ts) | ||
|
||
def test_partial_slice_second_precision(self): | ||
rng = DatetimeIndex(start=datetime(2005, 1, 1, 0, 0, 59, | ||
microsecond=999990), | ||
|
@@ -4941,6 +5113,56 @@ def test_partial_slice_second_precision(self): | |
self.assertRaisesRegexp(KeyError, '2005-1-1 00:00:00', | ||
lambda: s['2005-1-1 00:00:00']) | ||
|
||
# GH14856 | ||
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. can you give a 1-2 lines about what are asserting here 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. Done. 67e6bab |
||
# DatetimeIndex without explicit freq | ||
# Without microseconds | ||
df = DataFrame({'a': [1, 2, 3]}, | ||
DatetimeIndex(['2011-12-31 23:59:59', | ||
'2012-01-01 00:00:00', | ||
'2012-01-01 00:00:01']), | ||
dtype=np.int64) | ||
|
||
self.assertEqual(df.index.resolution, 'second') | ||
|
||
# Timestamp with resolution 'second' | ||
# Should be exact match for series and raise KeyError for Frame | ||
for ts, expected in (('2011-12-31 23:59:59', 1), | ||
('2012-01-01 00:00:00', 2), | ||
('2012-01-01 00:00:01', 3)): | ||
result = df['a'][ts] | ||
self.assertIsInstance(result, np.int64) | ||
self.assertEqual(result, expected) | ||
self.assertRaises(KeyError, df.__getitem__, ts) | ||
|
||
# Timestamp with resolution less precise than 'minute' | ||
for ts in ('2011', '2011-12', '2011-12-31', '2011-12-31 23', | ||
'2011-12-31 23:59'): | ||
# Series should return slice | ||
result = df['a'][ts] | ||
expected = df['a'][:1] | ||
assert_series_equal(result, expected) | ||
|
||
# Frame should return slice as well | ||
result = df[ts] | ||
expected = df[:1] | ||
assert_frame_equal(result, expected) | ||
|
||
# The same as previous but several elements in the slice | ||
for ts in ('2012', '2012-01', '2012-01-01', '2012-01-01 00', | ||
'2012-01-01 00:00'): | ||
# Series should return slice | ||
result = df['a'][ts] | ||
expected = df['a'][1:] | ||
assert_series_equal(result, expected) | ||
|
||
# Frame should return slice as well | ||
result = df[ts] | ||
expected = df[1:] | ||
assert_frame_equal(result, expected) | ||
|
||
# Not possible to create a string that represents timestamp | ||
# that is more exact then 'second' | ||
|
||
def test_partial_slicing_with_multiindex(self): | ||
|
||
# GH 4758 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
you are repeating lots of tests with only a slight variation
can u so it instead with a loop
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 agree. Actually, it was the reason why I wrote those helper functions initially.
Okay, I united tests for all resolutions into one loop. IMO it become less readable, but reflects the logic properly.