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 ()
@@ -747,7 +750,8 @@ def test_unstack_multi_level_rows_and_cols(self):
747
750
expected = df .unstack (["i3" ]).unstack (["i2" ])
748
751
tm .assert_frame_equal (result , expected )
749
752
750
- def test_unstack_nan_index (self ): # GH7466
753
+ def test_unstack_nan_index1 (self ):
754
+ # GH7466
751
755
def cast (val ):
752
756
val_str = "" if val != val else val
753
757
return f"{ val_str :1} "
@@ -833,6 +837,7 @@ def verify(df):
833
837
for col in ["4th" , "5th" ]:
834
838
verify (udf [col ])
835
839
840
+ def test_unstack_nan_index2 (self ):
836
841
# GH7403
837
842
df = DataFrame ({"A" : list ("aaaabbbb" ), "B" : range (8 ), "C" : range (8 )})
838
843
df .iloc [3 , 1 ] = np .NaN
@@ -875,6 +880,7 @@ def verify(df):
875
880
right = DataFrame (vals , columns = cols , index = idx )
876
881
tm .assert_frame_equal (left , right )
877
882
883
+ def test_unstack_nan_index3 (self , using_array_manager ):
878
884
# GH7401
879
885
df = DataFrame (
880
886
{
@@ -896,8 +902,13 @@ def verify(df):
896
902
)
897
903
898
904
right = DataFrame (vals , columns = cols , index = idx )
905
+ if using_array_manager :
906
+ # INFO(ArrayManager) with ArrayManager preserve dtype where possible
907
+ cols = right .columns [[1 , 2 , 3 , 5 ]]
908
+ right [cols ] = right [cols ].astype ("int64" )
899
909
tm .assert_frame_equal (left , right )
900
910
911
+ def test_unstack_nan_index4 (self ):
901
912
# GH4862
902
913
vals = [
903
914
["Hg" , np .nan , np .nan , 680585148 ],
@@ -938,6 +949,8 @@ def verify(df):
938
949
left = df .loc [17264 :].copy ().set_index (["s_id" , "dosage" , "agent" ])
939
950
tm .assert_frame_equal (left .unstack (), right )
940
951
952
+ @td .skip_array_manager_not_yet_implemented # TODO(ArrayManager) MultiIndex bug
953
+ def test_unstack_nan_index5 (self ):
941
954
# GH9497 - multiple unstack with nulls
942
955
df = DataFrame (
943
956
{
@@ -1887,7 +1900,7 @@ def test_unstack_group_index_overflow(self):
1887
1900
result = s .unstack (4 )
1888
1901
assert result .shape == (500 , 2 )
1889
1902
1890
- def test_unstack_with_missing_int_cast_to_float (self ):
1903
+ def test_unstack_with_missing_int_cast_to_float (self , using_array_manager ):
1891
1904
# https://github.com/pandas-dev/pandas/issues/37115
1892
1905
df = DataFrame (
1893
1906
{
@@ -1899,7 +1912,8 @@ def test_unstack_with_missing_int_cast_to_float(self):
1899
1912
1900
1913
# add another int column to get 2 blocks
1901
1914
df ["is_" ] = 1
1902
- assert len (df ._mgr .blocks ) == 2
1915
+ if not using_array_manager :
1916
+ assert len (df ._mgr .blocks ) == 2
1903
1917
1904
1918
result = df .unstack ("b" )
1905
1919
result [("is_" , "ca" )] = result [("is_" , "ca" )].fillna (0 )
@@ -1912,6 +1926,10 @@ def test_unstack_with_missing_int_cast_to_float(self):
1912
1926
names = [None , "b" ],
1913
1927
),
1914
1928
)
1929
+ if using_array_manager :
1930
+ # INFO(ArrayManager) with ArrayManager preserve dtype where possible
1931
+ expected [("v" , "cb" )] = expected [("v" , "cb" )].astype ("int64" )
1932
+ expected [("is_" , "cb" )] = expected [("is_" , "cb" )].astype ("int64" )
1915
1933
tm .assert_frame_equal (result , expected )
1916
1934
1917
1935
def test_unstack_with_level_has_nan (self ):
0 commit comments