Skip to content

Commit 67e6bab

Browse files
committed
Addressing code review: more comments added
1 parent c901588 commit 67e6bab

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

pandas/tseries/tests/test_timeseries.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -4942,12 +4942,18 @@ def test_partial_slice_second_precision(self):
49424942

49434943
def test_partial_slicing_dataframe(self):
49444944
# GH14856
4945-
# Test various combinations of string slicing
4945+
# Test various combinations of string slicing resolution vs.
4946+
# index resolution
4947+
# - If string resolution is less precise than index resolution,
4948+
# string is considered a slice
4949+
# - If string resolution is equal to or more precise than index
4950+
# resolution, string is considered an exact match
49464951
formats = ['%Y', '%Y-%m', '%Y-%m-%d', '%Y-%m-%d %H',
49474952
'%Y-%m-%d %H:%M', '%Y-%m-%d %H:%M:%S']
49484953
resolutions = ['year', 'month', 'day', 'hour', 'minute', 'second']
49494954
for rnum, resolution in enumerate(resolutions[2:], 2):
4950-
unit = Timedelta(1, resolution[0])
4955+
# we check only 'day', 'hour', 'minute' and 'second'
4956+
unit = Timedelta("1 " + resolution)
49514957
middate = datetime(2012, 1, 1, 0, 0, 0)
49524958
index = DatetimeIndex([middate - unit,
49534959
middate, middate + unit])
@@ -4956,7 +4962,8 @@ def test_partial_slicing_dataframe(self):
49564962
self.assertEqual(df.index.resolution, resolution)
49574963

49584964
# Timestamp with the same resolution as index
4959-
# Should be exact match for series and raise KeyError for Frame
4965+
# Should be exact match for Series (return scalar)
4966+
# and raise KeyError for Frame
49604967
for timestamp, expected in zip(index, values):
49614968
ts_string = timestamp.strftime(formats[rnum])
49624969
# make ts_string as precise as index
@@ -4970,6 +4977,7 @@ def test_partial_slicing_dataframe(self):
49704977
for element, theslice in [[0, slice(None, 1)],
49714978
[1, slice(1, None)]]:
49724979
ts_string = index[element].strftime(fmt)
4980+
49734981
# Series should return slice
49744982
result = df['a'][ts_string]
49754983
expected = df['a'][theslice]
@@ -4982,6 +4990,8 @@ def test_partial_slicing_dataframe(self):
49824990

49834991
# Timestamp with resolution more precise than index
49844992
# Compatible with existing key
4993+
# Should return scalar for Series
4994+
# and raise KeyError for Frame
49854995
for fmt in formats[rnum + 1:]:
49864996
ts_string = index[1].strftime(fmt)
49874997
result = df['a'][ts_string]
@@ -4990,8 +5000,9 @@ def test_partial_slicing_dataframe(self):
49905000
self.assertRaises(KeyError, df.__getitem__, ts_string)
49915001

49925002
# Not compatible with existing key
5003+
# Should raise KeyError
49935004
for fmt, res in list(zip(formats, resolutions))[rnum + 1:]:
4994-
ts = index[1] + Timedelta(1, res[0])
5005+
ts = index[1] + Timedelta("1 " + res)
49955006
ts_string = ts.strftime(fmt)
49965007
self.assertRaises(KeyError, df['a'].__getitem__, ts_string)
49975008
self.assertRaises(KeyError, df.__getitem__, ts_string)

0 commit comments

Comments
 (0)