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 (
10
12
DataFrame ,
@@ -60,12 +62,13 @@ def test_stack_mixed_level(self):
60
62
expected = expected [["a" , "b" ]]
61
63
tm .assert_frame_equal (result , expected )
62
64
63
- def test_unstack_not_consolidated (self ):
65
+ def test_unstack_not_consolidated (self , using_array_manager ):
64
66
# Gh#34708
65
67
df = DataFrame ({"x" : [1 , 2 , np .NaN ], "y" : [3.0 , 4 , np .NaN ]})
66
68
df2 = df [["x" ]]
67
69
df2 ["y" ] = df ["y" ]
68
- assert len (df2 ._mgr .blocks ) == 2
70
+ if not using_array_manager :
71
+ assert len (df2 ._mgr .blocks ) == 2
69
72
70
73
res = df2 .unstack ()
71
74
expected = df .unstack ()
@@ -118,6 +121,8 @@ def test_unstack_fill(self):
118
121
expected = unstacked ["w" ]
119
122
tm .assert_frame_equal (result , expected )
120
123
124
+ # TODO(ArrayManager) iset with multiple elements not yet implemented
125
+ @td .skip_array_manager_not_yet_implemented # TODO(ArrayManager) iset
121
126
def test_unstack_fill_frame (self ):
122
127
123
128
# From a dataframe
@@ -747,7 +752,8 @@ def test_unstack_multi_level_rows_and_cols(self):
747
752
expected = df .unstack (["i3" ]).unstack (["i2" ])
748
753
tm .assert_frame_equal (result , expected )
749
754
750
- def test_unstack_nan_index (self ): # GH7466
755
+ def test_unstack_nan_index1 (self ):
756
+ # GH7466
751
757
def cast (val ):
752
758
val_str = "" if val != val else val
753
759
return f"{ val_str :1} "
@@ -833,6 +839,7 @@ def verify(df):
833
839
for col in ["4th" , "5th" ]:
834
840
verify (udf [col ])
835
841
842
+ def test_unstack_nan_index2 (self ):
836
843
# GH7403
837
844
df = DataFrame ({"A" : list ("aaaabbbb" ), "B" : range (8 ), "C" : range (8 )})
838
845
df .iloc [3 , 1 ] = np .NaN
@@ -875,6 +882,7 @@ def verify(df):
875
882
right = DataFrame (vals , columns = cols , index = idx )
876
883
tm .assert_frame_equal (left , right )
877
884
885
+ def test_unstack_nan_index3 (self , using_array_manager ):
878
886
# GH7401
879
887
df = DataFrame (
880
888
{
@@ -896,8 +904,13 @@ def verify(df):
896
904
)
897
905
898
906
right = DataFrame (vals , columns = cols , index = idx )
907
+ if using_array_manager :
908
+ # with ArrayManager preserve dtype where possible
909
+ cols = right .columns [[1 , 2 , 3 , 5 ]]
910
+ right [cols ] = right [cols ].astype ("int64" )
899
911
tm .assert_frame_equal (left , right )
900
912
913
+ def test_unstack_nan_index4 (self ):
901
914
# GH4862
902
915
vals = [
903
916
["Hg" , np .nan , np .nan , 680585148 ],
@@ -938,6 +951,8 @@ def verify(df):
938
951
left = df .loc [17264 :].copy ().set_index (["s_id" , "dosage" , "agent" ])
939
952
tm .assert_frame_equal (left .unstack (), right )
940
953
954
+ @td .skip_array_manager_not_yet_implemented # TODO(ArrayManager) MultiIndex bug
955
+ def test_unstack_nan_index5 (self ):
941
956
# GH9497 - multiple unstack with nulls
942
957
df = DataFrame (
943
958
{
@@ -1453,6 +1468,7 @@ def test_stack_mixed_dtype(self, multiindex_dataframe_random_data):
1453
1468
assert result .name is None
1454
1469
assert stacked ["bar" ].dtype == np .float_
1455
1470
1471
+ @td .skip_array_manager_not_yet_implemented # TODO(ArrayManager) groupby
1456
1472
def test_unstack_bug (self ):
1457
1473
df = DataFrame (
1458
1474
{
@@ -1689,6 +1705,7 @@ def test_unstack_period_frame(self):
1689
1705
1690
1706
tm .assert_frame_equal (result3 , expected )
1691
1707
1708
+ @td .skip_array_manager_not_yet_implemented # TODO(ArrayManager) groupby
1692
1709
def test_stack_multiple_bug (self ):
1693
1710
# bug when some uniques are not present in the data GH#3170
1694
1711
id_col = ([1 ] * 3 ) + ([2 ] * 3 )
@@ -1887,7 +1904,7 @@ def test_unstack_group_index_overflow(self):
1887
1904
result = s .unstack (4 )
1888
1905
assert result .shape == (500 , 2 )
1889
1906
1890
- def test_unstack_with_missing_int_cast_to_float (self ):
1907
+ def test_unstack_with_missing_int_cast_to_float (self , using_array_manager ):
1891
1908
# https://github.com/pandas-dev/pandas/issues/37115
1892
1909
df = DataFrame (
1893
1910
{
@@ -1899,7 +1916,8 @@ def test_unstack_with_missing_int_cast_to_float(self):
1899
1916
1900
1917
# add another int column to get 2 blocks
1901
1918
df ["is_" ] = 1
1902
- assert len (df ._mgr .blocks ) == 2
1919
+ if not using_array_manager :
1920
+ assert len (df ._mgr .blocks ) == 2
1903
1921
1904
1922
result = df .unstack ("b" )
1905
1923
result [("is_" , "ca" )] = result [("is_" , "ca" )].fillna (0 )
@@ -1912,6 +1930,10 @@ def test_unstack_with_missing_int_cast_to_float(self):
1912
1930
names = [None , "b" ],
1913
1931
),
1914
1932
)
1933
+ if using_array_manager :
1934
+ # with ArrayManager preserve dtype where possible
1935
+ expected [("v" , "cb" )] = expected [("v" , "cb" )].astype ("int64" )
1936
+ expected [("is_" , "cb" )] = expected [("is_" , "cb" )].astype ("int64" )
1915
1937
tm .assert_frame_equal (result , expected )
1916
1938
1917
1939
def test_unstack_with_level_has_nan (self ):
0 commit comments