55import numpy as np
66import pytest
77
8+ import pandas .util ._test_decorators as td
9+
810import pandas as pd
911from pandas import DataFrame , Index , MultiIndex , Period , Series , Timedelta , date_range
1012import pandas ._testing as tm
@@ -52,12 +54,13 @@ def test_stack_mixed_level(self):
5254 expected = expected [["a" , "b" ]]
5355 tm .assert_frame_equal (result , expected )
5456
55- def test_unstack_not_consolidated (self ):
57+ def test_unstack_not_consolidated (self , using_array_manager ):
5658 # Gh#34708
5759 df = DataFrame ({"x" : [1 , 2 , np .NaN ], "y" : [3.0 , 4 , np .NaN ]})
5860 df2 = df [["x" ]]
5961 df2 ["y" ] = df ["y" ]
60- assert len (df2 ._mgr .blocks ) == 2
62+ if not using_array_manager :
63+ assert len (df2 ._mgr .blocks ) == 2
6164
6265 res = df2 .unstack ()
6366 expected = df .unstack ()
@@ -110,6 +113,8 @@ def test_unstack_fill(self):
110113 expected = unstacked ["w" ]
111114 tm .assert_frame_equal (result , expected )
112115
116+ # TODO(ArrayManager) iset with multiple elements not yet implemented
117+ @td .skip_array_manager_not_yet_implemented # TODO(ArrayManager) iset
113118 def test_unstack_fill_frame (self ):
114119
115120 # From a dataframe
@@ -739,7 +744,8 @@ def test_unstack_multi_level_rows_and_cols(self):
739744 expected = df .unstack (["i3" ]).unstack (["i2" ])
740745 tm .assert_frame_equal (result , expected )
741746
742- def test_unstack_nan_index (self ): # GH7466
747+ def test_unstack_nan_index1 (self ):
748+ # GH7466
743749 def cast (val ):
744750 val_str = "" if val != val else val
745751 return f"{ val_str :1} "
@@ -825,6 +831,7 @@ def verify(df):
825831 for col in ["4th" , "5th" ]:
826832 verify (udf [col ])
827833
834+ def test_unstack_nan_index2 (self ):
828835 # GH7403
829836 df = DataFrame ({"A" : list ("aaaabbbb" ), "B" : range (8 ), "C" : range (8 )})
830837 df .iloc [3 , 1 ] = np .NaN
@@ -867,6 +874,7 @@ def verify(df):
867874 right = DataFrame (vals , columns = cols , index = idx )
868875 tm .assert_frame_equal (left , right )
869876
877+ def test_unstack_nan_index3 (self , using_array_manager ):
870878 # GH7401
871879 df = DataFrame (
872880 {
@@ -888,8 +896,13 @@ def verify(df):
888896 )
889897
890898 right = DataFrame (vals , columns = cols , index = idx )
899+ if using_array_manager :
900+ # with ArrayManager preserve dtype where possible
901+ cols = right .columns [[1 , 2 , 3 , 5 ]]
902+ right [cols ] = right [cols ].astype ("int64" )
891903 tm .assert_frame_equal (left , right )
892904
905+ def test_unstack_nan_index4 (self ):
893906 # GH4862
894907 vals = [
895908 ["Hg" , np .nan , np .nan , 680585148 ],
@@ -930,6 +943,8 @@ def verify(df):
930943 left = df .loc [17264 :].copy ().set_index (["s_id" , "dosage" , "agent" ])
931944 tm .assert_frame_equal (left .unstack (), right )
932945
946+ @td .skip_array_manager_not_yet_implemented # TODO(ArrayManager) MultiIndex bug
947+ def test_unstack_nan_index5 (self ):
933948 # GH9497 - multiple unstack with nulls
934949 df = DataFrame (
935950 {
@@ -1447,6 +1462,7 @@ def test_stack_mixed_dtype(self, multiindex_dataframe_random_data):
14471462 assert result .name is None
14481463 assert stacked ["bar" ].dtype == np .float_
14491464
1465+ @td .skip_array_manager_not_yet_implemented # TODO(ArrayManager) groupby
14501466 def test_unstack_bug (self ):
14511467 df = DataFrame (
14521468 {
@@ -1683,6 +1699,7 @@ def test_unstack_period_frame(self):
16831699
16841700 tm .assert_frame_equal (result3 , expected )
16851701
1702+ @td .skip_array_manager_not_yet_implemented # TODO(ArrayManager) groupby
16861703 def test_stack_multiple_bug (self ):
16871704 # bug when some uniques are not present in the data GH#3170
16881705 id_col = ([1 ] * 3 ) + ([2 ] * 3 )
@@ -1881,7 +1898,7 @@ def test_unstack_group_index_overflow(self):
18811898 result = s .unstack (4 )
18821899 assert result .shape == (500 , 2 )
18831900
1884- def test_unstack_with_missing_int_cast_to_float (self ):
1901+ def test_unstack_with_missing_int_cast_to_float (self , using_array_manager ):
18851902 # https://github.com/pandas-dev/pandas/issues/37115
18861903 df = DataFrame (
18871904 {
@@ -1893,7 +1910,8 @@ def test_unstack_with_missing_int_cast_to_float(self):
18931910
18941911 # add another int column to get 2 blocks
18951912 df ["is_" ] = 1
1896- assert len (df ._mgr .blocks ) == 2
1913+ if not using_array_manager :
1914+ assert len (df ._mgr .blocks ) == 2
18971915
18981916 result = df .unstack ("b" )
18991917 result [("is_" , "ca" )] = result [("is_" , "ca" )].fillna (0 )
@@ -1906,6 +1924,10 @@ def test_unstack_with_missing_int_cast_to_float(self):
19061924 names = [None , "b" ],
19071925 ),
19081926 )
1927+ if using_array_manager :
1928+ # with ArrayManager preserve dtype where possible
1929+ expected [("v" , "cb" )] = expected [("v" , "cb" )].astype ("int64" )
1930+ expected [("is_" , "cb" )] = expected [("is_" , "cb" )].astype ("int64" )
19091931 tm .assert_frame_equal (result , expected )
19101932
19111933 def test_unstack_with_level_has_nan (self ):
0 commit comments