5
5
import numpy as np
6
6
import pytest
7
7
8
+ import pandas .util ._test_decorators as td
9
+
8
10
import pandas as pd
9
11
from pandas import DataFrame , Index , MultiIndex , Period , Series , Timedelta , date_range
10
12
import pandas ._testing as tm
@@ -52,12 +54,13 @@ def test_stack_mixed_level(self):
52
54
expected = expected [["a" , "b" ]]
53
55
tm .assert_frame_equal (result , expected )
54
56
55
- def test_unstack_not_consolidated (self ):
57
+ def test_unstack_not_consolidated (self , using_array_manager ):
56
58
# Gh#34708
57
59
df = DataFrame ({"x" : [1 , 2 , np .NaN ], "y" : [3.0 , 4 , np .NaN ]})
58
60
df2 = df [["x" ]]
59
61
df2 ["y" ] = df ["y" ]
60
- assert len (df2 ._mgr .blocks ) == 2
62
+ if not using_array_manager :
63
+ assert len (df2 ._mgr .blocks ) == 2
61
64
62
65
res = df2 .unstack ()
63
66
expected = df .unstack ()
@@ -110,6 +113,8 @@ def test_unstack_fill(self):
110
113
expected = unstacked ["w" ]
111
114
tm .assert_frame_equal (result , expected )
112
115
116
+ # TODO(ArrayManager) iset with multiple elements not yet implemented
117
+ @td .skip_array_manager_not_yet_implemented # TODO(ArrayManager) iset
113
118
def test_unstack_fill_frame (self ):
114
119
115
120
# From a dataframe
@@ -739,7 +744,8 @@ def test_unstack_multi_level_rows_and_cols(self):
739
744
expected = df .unstack (["i3" ]).unstack (["i2" ])
740
745
tm .assert_frame_equal (result , expected )
741
746
742
- def test_unstack_nan_index (self ): # GH7466
747
+ def test_unstack_nan_index1 (self ):
748
+ # GH7466
743
749
def cast (val ):
744
750
val_str = "" if val != val else val
745
751
return f"{ val_str :1} "
@@ -825,6 +831,7 @@ def verify(df):
825
831
for col in ["4th" , "5th" ]:
826
832
verify (udf [col ])
827
833
834
+ def test_unstack_nan_index2 (self ):
828
835
# GH7403
829
836
df = DataFrame ({"A" : list ("aaaabbbb" ), "B" : range (8 ), "C" : range (8 )})
830
837
df .iloc [3 , 1 ] = np .NaN
@@ -867,6 +874,7 @@ def verify(df):
867
874
right = DataFrame (vals , columns = cols , index = idx )
868
875
tm .assert_frame_equal (left , right )
869
876
877
+ def test_unstack_nan_index3 (self , using_array_manager ):
870
878
# GH7401
871
879
df = DataFrame (
872
880
{
@@ -888,8 +896,13 @@ def verify(df):
888
896
)
889
897
890
898
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" )
891
903
tm .assert_frame_equal (left , right )
892
904
905
+ def test_unstack_nan_index4 (self ):
893
906
# GH4862
894
907
vals = [
895
908
["Hg" , np .nan , np .nan , 680585148 ],
@@ -930,6 +943,8 @@ def verify(df):
930
943
left = df .loc [17264 :].copy ().set_index (["s_id" , "dosage" , "agent" ])
931
944
tm .assert_frame_equal (left .unstack (), right )
932
945
946
+ @td .skip_array_manager_not_yet_implemented # TODO(ArrayManager) MultiIndex bug
947
+ def test_unstack_nan_index5 (self ):
933
948
# GH9497 - multiple unstack with nulls
934
949
df = DataFrame (
935
950
{
@@ -1447,6 +1462,7 @@ def test_stack_mixed_dtype(self, multiindex_dataframe_random_data):
1447
1462
assert result .name is None
1448
1463
assert stacked ["bar" ].dtype == np .float_
1449
1464
1465
+ @td .skip_array_manager_not_yet_implemented # TODO(ArrayManager) groupby
1450
1466
def test_unstack_bug (self ):
1451
1467
df = DataFrame (
1452
1468
{
@@ -1683,6 +1699,7 @@ def test_unstack_period_frame(self):
1683
1699
1684
1700
tm .assert_frame_equal (result3 , expected )
1685
1701
1702
+ @td .skip_array_manager_not_yet_implemented # TODO(ArrayManager) groupby
1686
1703
def test_stack_multiple_bug (self ):
1687
1704
# bug when some uniques are not present in the data GH#3170
1688
1705
id_col = ([1 ] * 3 ) + ([2 ] * 3 )
@@ -1881,7 +1898,7 @@ def test_unstack_group_index_overflow(self):
1881
1898
result = s .unstack (4 )
1882
1899
assert result .shape == (500 , 2 )
1883
1900
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 ):
1885
1902
# https://github.com/pandas-dev/pandas/issues/37115
1886
1903
df = DataFrame (
1887
1904
{
@@ -1893,7 +1910,8 @@ def test_unstack_with_missing_int_cast_to_float(self):
1893
1910
1894
1911
# add another int column to get 2 blocks
1895
1912
df ["is_" ] = 1
1896
- assert len (df ._mgr .blocks ) == 2
1913
+ if not using_array_manager :
1914
+ assert len (df ._mgr .blocks ) == 2
1897
1915
1898
1916
result = df .unstack ("b" )
1899
1917
result [("is_" , "ca" )] = result [("is_" , "ca" )].fillna (0 )
@@ -1906,6 +1924,10 @@ def test_unstack_with_missing_int_cast_to_float(self):
1906
1924
names = [None , "b" ],
1907
1925
),
1908
1926
)
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" )
1909
1931
tm .assert_frame_equal (result , expected )
1910
1932
1911
1933
def test_unstack_with_level_has_nan (self ):
0 commit comments