@@ -602,7 +602,7 @@ def test_unstack_to_series(self, float_frame):
602
602
data = data .unstack ()
603
603
tm .assert_frame_equal (old_data , data )
604
604
605
- def test_unstack_dtypes (self ):
605
+ def test_unstack_dtypes (self , using_infer_string ):
606
606
# GH 2929
607
607
rows = [[1 , 1 , 3 , 4 ], [1 , 2 , 3 , 4 ], [2 , 1 , 3 , 4 ], [2 , 2 , 3 , 4 ]]
608
608
@@ -638,8 +638,9 @@ def test_unstack_dtypes(self):
638
638
df2 ["D" ] = "foo"
639
639
df3 = df2 .unstack ("B" )
640
640
result = df3 .dtypes
641
+ dtype = "string" if using_infer_string else np .dtype ("object" )
641
642
expected = Series (
642
- [np .dtype ("float64" )] * 2 + [np . dtype ( "object" ) ] * 2 ,
643
+ [np .dtype ("float64" )] * 2 + [dtype ] * 2 ,
643
644
index = MultiIndex .from_arrays (
644
645
[["C" , "C" , "D" , "D" ], [1 , 2 , 1 , 2 ]], names = (None , "B" )
645
646
),
@@ -1321,14 +1322,16 @@ def test_unstack_fill_frame_object():
1321
1322
# By default missing values will be NaN
1322
1323
result = data .unstack ()
1323
1324
expected = DataFrame (
1324
- {"a" : ["a" , np .nan , "a" ], "b" : ["b" , "c" , np .nan ]}, index = list ("xyz" )
1325
+ {"a" : ["a" , np .nan , "a" ], "b" : ["b" , "c" , np .nan ]},
1326
+ index = list ("xyz" ),
1327
+ dtype = object ,
1325
1328
)
1326
1329
tm .assert_frame_equal (result , expected )
1327
1330
1328
1331
# Fill with any value replaces missing values as expected
1329
1332
result = data .unstack (fill_value = "d" )
1330
1333
expected = DataFrame (
1331
- {"a" : ["a" , "d" , "a" ], "b" : ["b" , "c" , "d" ]}, index = list ("xyz" )
1334
+ {"a" : ["a" , "d" , "a" ], "b" : ["b" , "c" , "d" ]}, index = list ("xyz" ), dtype = object
1332
1335
)
1333
1336
tm .assert_frame_equal (result , expected )
1334
1337
@@ -2013,7 +2016,7 @@ def test_stack_multiple_bug(self, future_stack):
2013
2016
multi = df .set_index (["DATE" , "ID" ])
2014
2017
multi .columns .name = "Params"
2015
2018
unst = multi .unstack ("ID" )
2016
- msg = re .escape ("agg function failed [how->mean,dtype->object] " )
2019
+ msg = re .escape ("agg function failed [how->mean,dtype->" )
2017
2020
with pytest .raises (TypeError , match = msg ):
2018
2021
unst .resample ("W-THU" ).mean ()
2019
2022
down = unst .resample ("W-THU" ).mean (numeric_only = True )
@@ -2210,7 +2213,7 @@ def test_stack_unstack_unordered_multiindex(self, future_stack):
2210
2213
tm .assert_frame_equal (result , expected )
2211
2214
2212
2215
def test_unstack_preserve_types (
2213
- self , multiindex_year_month_day_dataframe_random_data
2216
+ self , multiindex_year_month_day_dataframe_random_data , using_infer_string
2214
2217
):
2215
2218
# GH#403
2216
2219
ymd = multiindex_year_month_day_dataframe_random_data
@@ -2219,7 +2222,11 @@ def test_unstack_preserve_types(
2219
2222
2220
2223
unstacked = ymd .unstack ("month" )
2221
2224
assert unstacked ["A" , 1 ].dtype == np .float64
2222
- assert unstacked ["E" , 1 ].dtype == np .object_
2225
+ assert (
2226
+ unstacked ["E" , 1 ].dtype == np .object_
2227
+ if not using_infer_string
2228
+ else "string"
2229
+ )
2223
2230
assert unstacked ["F" , 1 ].dtype == np .float64
2224
2231
2225
2232
def test_unstack_group_index_overflow (self , future_stack ):
@@ -2279,7 +2286,7 @@ def test_unstack_with_missing_int_cast_to_float(self, using_array_manager):
2279
2286
2280
2287
expected = DataFrame (
2281
2288
[[10.0 , 10.0 , 1.0 , 1.0 ], [np .nan , 10.0 , 0.0 , 1.0 ]],
2282
- index = Index (["A" , "B" ], dtype = "object" , name = "a" ),
2289
+ index = Index (["A" , "B" ], name = "a" ),
2283
2290
columns = MultiIndex .from_tuples (
2284
2291
[("v" , "ca" ), ("v" , "cb" ), ("is_" , "ca" ), ("is_" , "cb" )],
2285
2292
names = [None , "b" ],
0 commit comments