@@ -31,14 +31,23 @@ def assert_dtype(obj, expected_dtype):
31
31
"""
32
32
Helper to check the dtype for a Series, Index, or single-column DataFrame.
33
33
"""
34
- if isinstance (obj , DataFrame ):
35
- dtype = obj .dtypes .iat [0 ]
36
- else :
37
- dtype = obj .dtype
34
+ dtype = tm .get_dtype (obj )
38
35
39
36
assert dtype == expected_dtype
40
37
41
38
39
+ def get_expected_name (box , names ):
40
+ if box is DataFrame :
41
+ # Since we are operating with a DataFrame and a non-DataFrame,
42
+ # the non-DataFrame is cast to Series and its name ignored.
43
+ exname = names [0 ]
44
+ elif box in [tm .to_array , pd .array ]:
45
+ exname = names [1 ]
46
+ else :
47
+ exname = names [2 ]
48
+ return exname
49
+
50
+
42
51
# ------------------------------------------------------------------
43
52
# Timedelta64[ns] dtype Comparisons
44
53
@@ -1212,19 +1221,12 @@ def test_td64arr_add_sub_tdi(self, box_with_array, names):
1212
1221
# GH#17250 make sure result dtype is correct
1213
1222
# GH#19043 make sure names are propagated correctly
1214
1223
box = box_with_array
1224
+ exname = get_expected_name (box , names )
1215
1225
1216
- if box is pd .DataFrame and names [1 ] != names [0 ]:
1217
- pytest .skip (
1218
- "Name propagation for DataFrame does not behave like "
1219
- "it does for Index/Series"
1220
- )
1221
-
1222
- tdi = TimedeltaIndex (["0 days" , "1 day" ], name = names [0 ])
1226
+ tdi = TimedeltaIndex (["0 days" , "1 day" ], name = names [1 ])
1223
1227
tdi = np .array (tdi ) if box in [tm .to_array , pd .array ] else tdi
1224
- ser = Series ([Timedelta (hours = 3 ), Timedelta (hours = 4 )], name = names [1 ])
1225
- expected = Series (
1226
- [Timedelta (hours = 3 ), Timedelta (days = 1 , hours = 4 )], name = names [2 ]
1227
- )
1228
+ ser = Series ([Timedelta (hours = 3 ), Timedelta (hours = 4 )], name = names [0 ])
1229
+ expected = Series ([Timedelta (hours = 3 ), Timedelta (days = 1 , hours = 4 )], name = exname )
1228
1230
1229
1231
ser = tm .box_expected (ser , box )
1230
1232
expected = tm .box_expected (expected , box )
@@ -1238,7 +1240,7 @@ def test_td64arr_add_sub_tdi(self, box_with_array, names):
1238
1240
assert_dtype (result , "timedelta64[ns]" )
1239
1241
1240
1242
expected = Series (
1241
- [Timedelta (hours = - 3 ), Timedelta (days = 1 , hours = - 4 )], name = names [ 2 ]
1243
+ [Timedelta (hours = - 3 ), Timedelta (days = 1 , hours = - 4 )], name = exname
1242
1244
)
1243
1245
expected = tm .box_expected (expected , box )
1244
1246
@@ -1318,19 +1320,14 @@ def test_td64arr_sub_timedeltalike(self, two_hours, box_with_array):
1318
1320
def test_td64arr_add_offset_index (self , names , box_with_array ):
1319
1321
# GH#18849, GH#19744
1320
1322
box = box_with_array
1321
-
1322
- if box is pd .DataFrame and names [1 ] != names [0 ]:
1323
- pytest .skip (
1324
- "Name propagation for DataFrame does not behave like "
1325
- "it does for Index/Series"
1326
- )
1323
+ exname = get_expected_name (box , names )
1327
1324
1328
1325
tdi = TimedeltaIndex (["1 days 00:00:00" , "3 days 04:00:00" ], name = names [0 ])
1329
1326
other = pd .Index ([pd .offsets .Hour (n = 1 ), pd .offsets .Minute (n = - 2 )], name = names [1 ])
1330
1327
other = np .array (other ) if box in [tm .to_array , pd .array ] else other
1331
1328
1332
1329
expected = TimedeltaIndex (
1333
- [tdi [n ] + other [n ] for n in range (len (tdi ))], freq = "infer" , name = names [ 2 ]
1330
+ [tdi [n ] + other [n ] for n in range (len (tdi ))], freq = "infer" , name = exname
1334
1331
)
1335
1332
tdi = tm .box_expected (tdi , box )
1336
1333
expected = tm .box_expected (expected , box )
@@ -1370,13 +1367,7 @@ def test_td64arr_sub_offset_index(self, names, box_with_array):
1370
1367
# GH#18824, GH#19744
1371
1368
box = box_with_array
1372
1369
xbox = box if box not in [tm .to_array , pd .array ] else pd .Index
1373
- exname = names [2 ] if box not in [tm .to_array , pd .array ] else names [1 ]
1374
-
1375
- if box is pd .DataFrame and names [1 ] != names [0 ]:
1376
- pytest .skip (
1377
- "Name propagation for DataFrame does not behave like "
1378
- "it does for Index/Series"
1379
- )
1370
+ exname = get_expected_name (box , names )
1380
1371
1381
1372
tdi = TimedeltaIndex (["1 days 00:00:00" , "3 days 04:00:00" ], name = names [0 ])
1382
1373
other = pd .Index ([pd .offsets .Hour (n = 1 ), pd .offsets .Minute (n = - 2 )], name = names [1 ])
@@ -1412,15 +1403,7 @@ def test_td64arr_with_offset_series(self, names, box_with_array):
1412
1403
# GH#18849
1413
1404
box = box_with_array
1414
1405
box2 = Series if box in [pd .Index , tm .to_array , pd .array ] else box
1415
-
1416
- if box is pd .DataFrame :
1417
- # Since we are operating with a DataFrame and a non-DataFrame,
1418
- # the non-DataFrame is cast to Series and its name ignored.
1419
- exname = names [0 ]
1420
- elif box in [tm .to_array , pd .array ]:
1421
- exname = names [1 ]
1422
- else :
1423
- exname = names [2 ]
1406
+ exname = get_expected_name (box , names )
1424
1407
1425
1408
tdi = TimedeltaIndex (["1 days 00:00:00" , "3 days 04:00:00" ], name = names [0 ])
1426
1409
other = Series ([pd .offsets .Hour (n = 1 ), pd .offsets .Minute (n = - 2 )], name = names [1 ])
@@ -2100,11 +2083,7 @@ def test_td64arr_div_numeric_array(self, box_with_array, vector, any_real_dtype)
2100
2083
def test_td64arr_mul_int_series (self , box_with_array , names , request ):
2101
2084
# GH#19042 test for correct name attachment
2102
2085
box = box_with_array
2103
- if box_with_array is pd .DataFrame and names [2 ] is None :
2104
- reason = "broadcasts along wrong axis, but doesn't raise"
2105
- request .node .add_marker (pytest .mark .xfail (reason = reason ))
2106
-
2107
- exname = names [2 ] if box not in [tm .to_array , pd .array ] else names [1 ]
2086
+ exname = get_expected_name (box , names )
2108
2087
2109
2088
tdi = TimedeltaIndex (
2110
2089
["0days" , "1day" , "2days" , "3days" , "4days" ], name = names [0 ]
@@ -2119,11 +2098,8 @@ def test_td64arr_mul_int_series(self, box_with_array, names, request):
2119
2098
)
2120
2099
2121
2100
tdi = tm .box_expected (tdi , box )
2122
- xbox = (
2123
- Series
2124
- if (box is pd .Index or box is tm .to_array or box is pd .array )
2125
- else box
2126
- )
2101
+ xbox = get_upcast_box (box , ser )
2102
+
2127
2103
expected = tm .box_expected (expected , xbox )
2128
2104
2129
2105
result = ser * tdi
@@ -2154,9 +2130,7 @@ def test_float_series_rdiv_td64arr(self, box_with_array, names):
2154
2130
name = xname ,
2155
2131
)
2156
2132
2157
- xbox = box
2158
- if box in [pd .Index , tm .to_array , pd .array ] and type (ser ) is Series :
2159
- xbox = Series
2133
+ xbox = get_upcast_box (box , ser )
2160
2134
2161
2135
tdi = tm .box_expected (tdi , box )
2162
2136
expected = tm .box_expected (expected , xbox )
0 commit comments