@@ -4942,12 +4942,18 @@ def test_partial_slice_second_precision(self):
4942
4942
4943
4943
def test_partial_slicing_dataframe (self ):
4944
4944
# 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
4946
4951
formats = ['%Y' , '%Y-%m' , '%Y-%m-%d' , '%Y-%m-%d %H' ,
4947
4952
'%Y-%m-%d %H:%M' , '%Y-%m-%d %H:%M:%S' ]
4948
4953
resolutions = ['year' , 'month' , 'day' , 'hour' , 'minute' , 'second' ]
4949
4954
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 )
4951
4957
middate = datetime (2012 , 1 , 1 , 0 , 0 , 0 )
4952
4958
index = DatetimeIndex ([middate - unit ,
4953
4959
middate , middate + unit ])
@@ -4956,7 +4962,8 @@ def test_partial_slicing_dataframe(self):
4956
4962
self .assertEqual (df .index .resolution , resolution )
4957
4963
4958
4964
# 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
4960
4967
for timestamp , expected in zip (index , values ):
4961
4968
ts_string = timestamp .strftime (formats [rnum ])
4962
4969
# make ts_string as precise as index
@@ -4970,6 +4977,7 @@ def test_partial_slicing_dataframe(self):
4970
4977
for element , theslice in [[0 , slice (None , 1 )],
4971
4978
[1 , slice (1 , None )]]:
4972
4979
ts_string = index [element ].strftime (fmt )
4980
+
4973
4981
# Series should return slice
4974
4982
result = df ['a' ][ts_string ]
4975
4983
expected = df ['a' ][theslice ]
@@ -4982,6 +4990,8 @@ def test_partial_slicing_dataframe(self):
4982
4990
4983
4991
# Timestamp with resolution more precise than index
4984
4992
# Compatible with existing key
4993
+ # Should return scalar for Series
4994
+ # and raise KeyError for Frame
4985
4995
for fmt in formats [rnum + 1 :]:
4986
4996
ts_string = index [1 ].strftime (fmt )
4987
4997
result = df ['a' ][ts_string ]
@@ -4990,8 +5000,9 @@ def test_partial_slicing_dataframe(self):
4990
5000
self .assertRaises (KeyError , df .__getitem__ , ts_string )
4991
5001
4992
5002
# Not compatible with existing key
5003
+ # Should raise KeyError
4993
5004
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 )
4995
5006
ts_string = ts .strftime (fmt )
4996
5007
self .assertRaises (KeyError , df ['a' ].__getitem__ , ts_string )
4997
5008
self .assertRaises (KeyError , df .__getitem__ , ts_string )
0 commit comments