Skip to content

Commit c901588

Browse files
committed
Addressing code review: testing different combinations with the loop instead of copy-pasting of the code
1 parent 9b55117 commit c901588

File tree

1 file changed

+54
-221
lines changed

1 file changed

+54
-221
lines changed

pandas/tseries/tests/test_timeseries.py

+54-221
Original file line numberDiff line numberDiff line change
@@ -4896,64 +4896,6 @@ def test_partial_slice_daily(self):
48964896

48974897
self.assertRaises(Exception, s.__getitem__, '2004-12-31 00')
48984898

4899-
# GH14856
4900-
# DatetimeIndex without explicit freq
4901-
df = DataFrame({'a': [1, 2, 3]}, DatetimeIndex(['2011-12-31',
4902-
'2012-01-01',
4903-
'2012-01-02']),
4904-
dtype=np.int64)
4905-
4906-
self.assertEqual(df.index.resolution, 'day')
4907-
4908-
# Timestamp with resolution 'day'
4909-
# Should be exact match for series and raise KeyError for Frame
4910-
for ts, expected in (('2011-12-31', 1), ('2012-01-01', 2),
4911-
('2012-01-02', 3)):
4912-
result = df['a'][ts]
4913-
self.assertIsInstance(result, np.int64)
4914-
self.assertEqual(result, expected)
4915-
self.assertRaises(KeyError, df.__getitem__, ts)
4916-
4917-
# Timestamp with resolution less precise than 'day'
4918-
for ts in ('2011', '2011-12'):
4919-
# Series should return slice
4920-
result = df['a'][ts]
4921-
expected = df['a'][:1]
4922-
assert_series_equal(result, expected)
4923-
4924-
# Frame should return slice as well
4925-
result = df[ts]
4926-
expected = df[:1]
4927-
assert_frame_equal(result, expected)
4928-
4929-
# The same as previous but several elements in the slice
4930-
for ts in ('2012', '2012-01'):
4931-
# Series should return slice
4932-
result = df['a'][ts]
4933-
expected = df['a'][1:]
4934-
assert_series_equal(result, expected)
4935-
4936-
# Frame should return slice as well
4937-
result = df[ts]
4938-
expected = df[1:]
4939-
assert_frame_equal(result, expected)
4940-
4941-
# Timestamp with resolution more precise than 'day'
4942-
# Compatible with existing key
4943-
for ts in ('2012-01-01 00', '2012-01-01 00:00',
4944-
'2012-01-01 00:00:00'):
4945-
result = df['a'][ts]
4946-
self.assertIsInstance(result, np.int64)
4947-
self.assertEqual(result, 2)
4948-
self.assertRaises(KeyError, df.__getitem__, ts)
4949-
4950-
# Timestamp with resolution more precise than 'day'
4951-
# Not compatible with existing key
4952-
for ts in ('2012-01-01 01', '2012-01-01 00:01',
4953-
'2012-01-01 00:00:01'):
4954-
self.assertRaises(KeyError, df['a'].__getitem__, ts)
4955-
self.assertRaises(KeyError, df.__getitem__, ts)
4956-
49574899
def test_partial_slice_hourly(self):
49584900
rng = DatetimeIndex(freq='T', start=datetime(2005, 1, 1, 20, 0, 0),
49594901
periods=500)
@@ -4968,63 +4910,6 @@ def test_partial_slice_hourly(self):
49684910
self.assertEqual(s['2005-1-1 20:00'], s.ix[0])
49694911
self.assertRaises(Exception, s.__getitem__, '2004-12-31 00:15')
49704912

4971-
# GH14856
4972-
# DatetimeIndex without explicit freq
4973-
df = DataFrame({'a': [1, 2, 3]}, DatetimeIndex(['2011-12-31 23',
4974-
'2012-01-01 00',
4975-
'2012-01-01 01']),
4976-
dtype=np.int64)
4977-
4978-
self.assertEqual(df.index.resolution, 'hour')
4979-
4980-
# Timestamp with resolution 'hour'
4981-
# Should be exact match for series and raise KeyError for Frame
4982-
for ts, expected in (('2011-12-31 23', 1),
4983-
('2012-01-01 00', 2),
4984-
('2012-01-01 01', 3)):
4985-
result = df['a'][ts]
4986-
self.assertIsInstance(result, np.int64)
4987-
self.assertEqual(result, expected)
4988-
self.assertRaises(KeyError, df.__getitem__, ts)
4989-
4990-
# Timestamp with resolution less precise than 'hour'
4991-
for ts in ('2011', '2011-12', '2011-12-31'):
4992-
# Series should return slice
4993-
result = df['a'][ts]
4994-
expected = df['a'][:1]
4995-
assert_series_equal(result, expected)
4996-
4997-
# Frame should return slice as well
4998-
result = df[ts]
4999-
expected = df[:1]
5000-
assert_frame_equal(result, expected)
5001-
5002-
# The same as previous but several elements in the slice
5003-
for ts in ('2012', '2012-01', '2012-01-01'):
5004-
# Series should return slice
5005-
result = df['a'][ts]
5006-
expected = df['a'][1:]
5007-
assert_series_equal(result, expected)
5008-
5009-
# Frame should return slice as well
5010-
result = df[ts]
5011-
expected = df[1:]
5012-
assert_frame_equal(result, expected)
5013-
5014-
# Timestamp with resolution more precise than 'hour'
5015-
# Compatible with existing key
5016-
for ts in ('2012-01-01 00:00', '2012-01-01 00:00:00'):
5017-
result = df['a'][ts]
5018-
self.assertIsInstance(result, np.int64)
5019-
self.assertEqual(result, 2)
5020-
self.assertRaises(KeyError, df.__getitem__, ts)
5021-
5022-
# Timestamp with resolution more precise than 'day'
5023-
# Not compatible with existing key
5024-
for ts in ('2012-01-01 00:01', '2012-01-01 00:00:01'):
5025-
self.assertRaises(KeyError, df['a'].__getitem__, ts)
5026-
self.assertRaises(KeyError, df.__getitem__, ts)
5027-
50284913
def test_partial_slice_minutely(self):
50294914
rng = DatetimeIndex(freq='S', start=datetime(2005, 1, 1, 23, 59, 0),
50304915
periods=500)
@@ -5039,64 +4924,6 @@ def test_partial_slice_minutely(self):
50394924
self.assertEqual(s[Timestamp('2005-1-1 23:59:00')], s.ix[0])
50404925
self.assertRaises(Exception, s.__getitem__, '2004-12-31 00:00:00')
50414926

5042-
# GH14856
5043-
# DatetimeIndex without explicit freq
5044-
df = DataFrame({'a': [1, 2, 3]},
5045-
DatetimeIndex(['2011-12-31 23:59',
5046-
'2012-01-01 00:00',
5047-
'2012-01-01 00:01']),
5048-
dtype=np.int64)
5049-
5050-
self.assertEqual(df.index.resolution, 'minute')
5051-
5052-
# Timestamp with resolution 'minute'
5053-
# Should be exact match for series and raise KeyError for Frame
5054-
for ts, expected in (('2011-12-31 23:59', 1),
5055-
('2012-01-01 00:00', 2),
5056-
('2012-01-01 00:01', 3)):
5057-
result = df['a'][ts]
5058-
self.assertIsInstance(result, np.int64)
5059-
self.assertEqual(result, expected)
5060-
self.assertRaises(KeyError, df.__getitem__, ts)
5061-
5062-
# Timestamp with resolution less precise than 'minute'
5063-
for ts in ('2011', '2011-12', '2011-12-31', '2011-12-31 23'):
5064-
# Series should return slice
5065-
result = df['a'][ts]
5066-
expected = df['a'][:1]
5067-
assert_series_equal(result, expected)
5068-
5069-
# Frame should return slice as well
5070-
result = df[ts]
5071-
expected = df[:1]
5072-
assert_frame_equal(result, expected)
5073-
5074-
# The same as previous but several elements in the slice
5075-
for ts in ('2012', '2012-01', '2012-01-01', '2012-01-01 00'):
5076-
# Series should return slice
5077-
result = df['a'][ts]
5078-
expected = df['a'][1:]
5079-
assert_series_equal(result, expected)
5080-
5081-
# Frame should return slice as well
5082-
result = df[ts]
5083-
expected = df[1:]
5084-
assert_frame_equal(result, expected)
5085-
5086-
# Timestamp with resolution more precise than 'minute'
5087-
# Compatible with existing key
5088-
ts = '2012-01-01 00:00:00'
5089-
result = df['a'][ts]
5090-
self.assertIsInstance(result, np.int64)
5091-
self.assertEqual(result, 2)
5092-
self.assertRaises(KeyError, df.__getitem__, ts)
5093-
5094-
# Timestamp with resolution more precise than 'day'
5095-
# Not compatible with existing key
5096-
ts = '2012-01-01 00:00:01'
5097-
self.assertRaises(KeyError, df['a'].__getitem__, ts)
5098-
self.assertRaises(KeyError, df.__getitem__, ts)
5099-
51004927
def test_partial_slice_second_precision(self):
51014928
rng = DatetimeIndex(start=datetime(2005, 1, 1, 0, 0, 59,
51024929
microsecond=999990),
@@ -5113,55 +4940,61 @@ def test_partial_slice_second_precision(self):
51134940
self.assertRaisesRegexp(KeyError, '2005-1-1 00:00:00',
51144941
lambda: s['2005-1-1 00:00:00'])
51154942

4943+
def test_partial_slicing_dataframe(self):
51164944
# GH14856
5117-
# DatetimeIndex without explicit freq
5118-
# Without microseconds
5119-
df = DataFrame({'a': [1, 2, 3]},
5120-
DatetimeIndex(['2011-12-31 23:59:59',
5121-
'2012-01-01 00:00:00',
5122-
'2012-01-01 00:00:01']),
5123-
dtype=np.int64)
5124-
5125-
self.assertEqual(df.index.resolution, 'second')
5126-
5127-
# Timestamp with resolution 'second'
5128-
# Should be exact match for series and raise KeyError for Frame
5129-
for ts, expected in (('2011-12-31 23:59:59', 1),
5130-
('2012-01-01 00:00:00', 2),
5131-
('2012-01-01 00:00:01', 3)):
5132-
result = df['a'][ts]
5133-
self.assertIsInstance(result, np.int64)
5134-
self.assertEqual(result, expected)
5135-
self.assertRaises(KeyError, df.__getitem__, ts)
5136-
5137-
# Timestamp with resolution less precise than 'minute'
5138-
for ts in ('2011', '2011-12', '2011-12-31', '2011-12-31 23',
5139-
'2011-12-31 23:59'):
5140-
# Series should return slice
5141-
result = df['a'][ts]
5142-
expected = df['a'][:1]
5143-
assert_series_equal(result, expected)
5144-
5145-
# Frame should return slice as well
5146-
result = df[ts]
5147-
expected = df[:1]
5148-
assert_frame_equal(result, expected)
5149-
5150-
# The same as previous but several elements in the slice
5151-
for ts in ('2012', '2012-01', '2012-01-01', '2012-01-01 00',
5152-
'2012-01-01 00:00'):
5153-
# Series should return slice
5154-
result = df['a'][ts]
5155-
expected = df['a'][1:]
5156-
assert_series_equal(result, expected)
5157-
5158-
# Frame should return slice as well
5159-
result = df[ts]
5160-
expected = df[1:]
5161-
assert_frame_equal(result, expected)
5162-
5163-
# Not possible to create a string that represents timestamp
5164-
# that is more exact then 'second'
4945+
# Test various combinations of string slicing
4946+
formats = ['%Y', '%Y-%m', '%Y-%m-%d', '%Y-%m-%d %H',
4947+
'%Y-%m-%d %H:%M', '%Y-%m-%d %H:%M:%S']
4948+
resolutions = ['year', 'month', 'day', 'hour', 'minute', 'second']
4949+
for rnum, resolution in enumerate(resolutions[2:], 2):
4950+
unit = Timedelta(1, resolution[0])
4951+
middate = datetime(2012, 1, 1, 0, 0, 0)
4952+
index = DatetimeIndex([middate - unit,
4953+
middate, middate + unit])
4954+
values = [1, 2, 3]
4955+
df = DataFrame({'a': values}, index, dtype=np.int64)
4956+
self.assertEqual(df.index.resolution, resolution)
4957+
4958+
# Timestamp with the same resolution as index
4959+
# Should be exact match for series and raise KeyError for Frame
4960+
for timestamp, expected in zip(index, values):
4961+
ts_string = timestamp.strftime(formats[rnum])
4962+
# make ts_string as precise as index
4963+
result = df['a'][ts_string]
4964+
self.assertIsInstance(result, np.int64)
4965+
self.assertEqual(result, expected)
4966+
self.assertRaises(KeyError, df.__getitem__, ts_string)
4967+
4968+
# Timestamp with resolution less precise than index
4969+
for fmt in formats[:rnum]:
4970+
for element, theslice in [[0, slice(None, 1)],
4971+
[1, slice(1, None)]]:
4972+
ts_string = index[element].strftime(fmt)
4973+
# Series should return slice
4974+
result = df['a'][ts_string]
4975+
expected = df['a'][theslice]
4976+
assert_series_equal(result, expected)
4977+
4978+
# Frame should return slice as well
4979+
result = df[ts_string]
4980+
expected = df[theslice]
4981+
assert_frame_equal(result, expected)
4982+
4983+
# Timestamp with resolution more precise than index
4984+
# Compatible with existing key
4985+
for fmt in formats[rnum + 1:]:
4986+
ts_string = index[1].strftime(fmt)
4987+
result = df['a'][ts_string]
4988+
self.assertIsInstance(result, np.int64)
4989+
self.assertEqual(result, 2)
4990+
self.assertRaises(KeyError, df.__getitem__, ts_string)
4991+
4992+
# Not compatible with existing key
4993+
for fmt, res in list(zip(formats, resolutions))[rnum + 1:]:
4994+
ts = index[1] + Timedelta(1, res[0])
4995+
ts_string = ts.strftime(fmt)
4996+
self.assertRaises(KeyError, df['a'].__getitem__, ts_string)
4997+
self.assertRaises(KeyError, df.__getitem__, ts_string)
51654998

51664999
def test_partial_slicing_with_multiindex(self):
51675000

0 commit comments

Comments
 (0)