@@ -1653,6 +1653,77 @@ def test_handle_empty_objects(self):
1653
1653
1654
1654
tm .assert_frame_equal (concatted , expected )
1655
1655
1656
+ # empty as first element with time series
1657
+ # GH3259
1658
+ df = DataFrame (dict (A = range (10000 )),index = date_range ('20130101' ,periods = 10000 ,freq = 's' ))
1659
+ empty = DataFrame ()
1660
+ result = concat ([df ,empty ],axis = 1 )
1661
+ assert_frame_equal (result , df )
1662
+ result = concat ([empty ,df ],axis = 1 )
1663
+ assert_frame_equal (result , df )
1664
+
1665
+ result = concat ([df ,empty ])
1666
+ assert_frame_equal (result , df )
1667
+ result = concat ([empty ,df ])
1668
+ assert_frame_equal (result , df )
1669
+
1670
+ def test_concat_mixed_objs (self ):
1671
+
1672
+ # concat mixed series/frames
1673
+ # G2385
1674
+
1675
+ # axis 1
1676
+ index = date_range ('01-Jan-2013' , periods = 10 , freq = 'H' )
1677
+ arr = np .arange (10 , dtype = 'int64' )
1678
+ s1 = Series (arr , index = index )
1679
+ s2 = Series (arr , index = index )
1680
+ df = DataFrame (arr .reshape (- 1 ,1 ), index = index )
1681
+
1682
+ expected = DataFrame (np .repeat (arr ,2 ).reshape (- 1 ,2 ), index = index , columns = [0 , 0 ])
1683
+ result = concat ([df ,df ], axis = 1 )
1684
+ assert_frame_equal (result , expected )
1685
+
1686
+ expected = DataFrame (np .repeat (arr ,2 ).reshape (- 1 ,2 ), index = index , columns = [0 , 1 ])
1687
+ result = concat ([s1 ,s2 ], axis = 1 )
1688
+ assert_frame_equal (result , expected )
1689
+
1690
+ expected = DataFrame (np .repeat (arr ,3 ).reshape (- 1 ,3 ), index = index , columns = [0 , 1 , 2 ])
1691
+ result = concat ([s1 ,s2 ,s1 ], axis = 1 )
1692
+ assert_frame_equal (result , expected )
1693
+
1694
+ expected = DataFrame (np .repeat (arr ,5 ).reshape (- 1 ,5 ), index = index , columns = [0 , 0 , 1 , 2 , 3 ])
1695
+ result = concat ([s1 ,df ,s2 ,s2 ,s1 ], axis = 1 )
1696
+ assert_frame_equal (result , expected )
1697
+
1698
+ # with names
1699
+ s1 .name = 'foo'
1700
+ expected = DataFrame (np .repeat (arr ,3 ).reshape (- 1 ,3 ), index = index , columns = ['foo' , 0 , 0 ])
1701
+ result = concat ([s1 ,df ,s2 ], axis = 1 )
1702
+ assert_frame_equal (result , expected )
1703
+
1704
+ s2 .name = 'bar'
1705
+ expected = DataFrame (np .repeat (arr ,3 ).reshape (- 1 ,3 ), index = index , columns = ['foo' , 0 , 'bar' ])
1706
+ result = concat ([s1 ,df ,s2 ], axis = 1 )
1707
+ assert_frame_equal (result , expected )
1708
+
1709
+ # ignore index
1710
+ expected = DataFrame (np .repeat (arr ,3 ).reshape (- 1 ,3 ), index = index , columns = [0 , 1 , 2 ])
1711
+ result = concat ([s1 ,df ,s2 ], axis = 1 , ignore_index = True )
1712
+ assert_frame_equal (result , expected )
1713
+
1714
+ # axis 0
1715
+ expected = DataFrame (np .tile (arr ,3 ).reshape (- 1 ,1 ), index = index .tolist () * 3 , columns = [0 ])
1716
+ result = concat ([s1 ,df ,s2 ])
1717
+ assert_frame_equal (result , expected )
1718
+
1719
+ expected = DataFrame (np .tile (arr ,3 ).reshape (- 1 ,1 ), columns = [0 ])
1720
+ result = concat ([s1 ,df ,s2 ], ignore_index = True )
1721
+ assert_frame_equal (result , expected )
1722
+
1723
+ # invalid concatente of mixed dims
1724
+ panel = tm .makePanel ()
1725
+ self .assertRaises (ValueError , lambda : concat ([panel ,s1 ],axis = 1 ))
1726
+
1656
1727
def test_panel_join (self ):
1657
1728
panel = tm .makePanel ()
1658
1729
tm .add_nans (panel )
@@ -1967,6 +2038,13 @@ def test_concat_series_axis1_same_names_ignore_index(self):
1967
2038
result = concat ([s1 , s2 ], axis = 1 , ignore_index = True )
1968
2039
self .assertTrue (np .array_equal (result .columns , [0 , 1 ]))
1969
2040
2041
+ def test_concat_invalid (self ):
2042
+
2043
+ # trying to concat a ndframe with a non-ndframe
2044
+ df1 = mkdf (10 , 2 )
2045
+ for obj in [1 , dict (), [1 , 2 ], (1 , 2 ) ]:
2046
+ self .assertRaises (TypeError , lambda x : concat ([ df1 , obj ]))
2047
+
1970
2048
def test_concat_invalid_first_argument (self ):
1971
2049
df1 = mkdf (10 , 2 )
1972
2050
df2 = mkdf (10 , 2 )
@@ -1975,15 +2053,6 @@ def test_concat_invalid_first_argument(self):
1975
2053
# generator ok though
1976
2054
concat (DataFrame (np .random .rand (5 ,5 )) for _ in range (3 ))
1977
2055
1978
- def test_concat_mixed_types_fails (self ):
1979
- df = DataFrame (randn (10 , 1 ))
1980
-
1981
- with tm .assertRaisesRegexp (TypeError , "Cannot concatenate.+" ):
1982
- concat ([df [0 ], df ], axis = 1 )
1983
-
1984
- with tm .assertRaisesRegexp (TypeError , "Cannot concatenate.+" ):
1985
- concat ([df , df [0 ]], axis = 1 )
1986
-
1987
2056
class TestOrderedMerge (tm .TestCase ):
1988
2057
1989
2058
def setUp (self ):
0 commit comments