@@ -31,14 +31,23 @@ def assert_dtype(obj, expected_dtype):
3131 """
3232 Helper to check the dtype for a Series, Index, or single-column DataFrame.
3333 """
34- if isinstance (obj , DataFrame ):
35- dtype = obj .dtypes .iat [0 ]
36- else :
37- dtype = obj .dtype
34+ dtype = tm .get_dtype (obj )
3835
3936 assert dtype == expected_dtype
4037
4138
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+
4251# ------------------------------------------------------------------
4352# Timedelta64[ns] dtype Comparisons
4453
@@ -1212,19 +1221,12 @@ def test_td64arr_add_sub_tdi(self, box_with_array, names):
12121221 # GH#17250 make sure result dtype is correct
12131222 # GH#19043 make sure names are propagated correctly
12141223 box = box_with_array
1224+ exname = get_expected_name (box , names )
12151225
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 ])
12231227 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 )
12281230
12291231 ser = tm .box_expected (ser , box )
12301232 expected = tm .box_expected (expected , box )
@@ -1238,7 +1240,7 @@ def test_td64arr_add_sub_tdi(self, box_with_array, names):
12381240 assert_dtype (result , "timedelta64[ns]" )
12391241
12401242 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
12421244 )
12431245 expected = tm .box_expected (expected , box )
12441246
@@ -1318,19 +1320,14 @@ def test_td64arr_sub_timedeltalike(self, two_hours, box_with_array):
13181320 def test_td64arr_add_offset_index (self , names , box_with_array ):
13191321 # GH#18849, GH#19744
13201322 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 )
13271324
13281325 tdi = TimedeltaIndex (["1 days 00:00:00" , "3 days 04:00:00" ], name = names [0 ])
13291326 other = pd .Index ([pd .offsets .Hour (n = 1 ), pd .offsets .Minute (n = - 2 )], name = names [1 ])
13301327 other = np .array (other ) if box in [tm .to_array , pd .array ] else other
13311328
13321329 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
13341331 )
13351332 tdi = tm .box_expected (tdi , box )
13361333 expected = tm .box_expected (expected , box )
@@ -1370,13 +1367,7 @@ def test_td64arr_sub_offset_index(self, names, box_with_array):
13701367 # GH#18824, GH#19744
13711368 box = box_with_array
13721369 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 )
13801371
13811372 tdi = TimedeltaIndex (["1 days 00:00:00" , "3 days 04:00:00" ], name = names [0 ])
13821373 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):
14121403 # GH#18849
14131404 box = box_with_array
14141405 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 )
14241407
14251408 tdi = TimedeltaIndex (["1 days 00:00:00" , "3 days 04:00:00" ], name = names [0 ])
14261409 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)
21002083 def test_td64arr_mul_int_series (self , box_with_array , names , request ):
21012084 # GH#19042 test for correct name attachment
21022085 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 )
21082087
21092088 tdi = TimedeltaIndex (
21102089 ["0days" , "1day" , "2days" , "3days" , "4days" ], name = names [0 ]
@@ -2119,11 +2098,8 @@ def test_td64arr_mul_int_series(self, box_with_array, names, request):
21192098 )
21202099
21212100 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+
21272103 expected = tm .box_expected (expected , xbox )
21282104
21292105 result = ser * tdi
@@ -2154,9 +2130,7 @@ def test_float_series_rdiv_td64arr(self, box_with_array, names):
21542130 name = xname ,
21552131 )
21562132
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 )
21602134
21612135 tdi = tm .box_expected (tdi , box )
21622136 expected = tm .box_expected (expected , xbox )
0 commit comments