@@ -2487,7 +2487,7 @@ def _set_item(self, key, value):
2487
2487
2488
2488
# check if we are modifying a copy
2489
2489
# try to set first as we want an invalid
2490
- # value exeption to occur first
2490
+ # value exception to occur first
2491
2491
if len (self ):
2492
2492
self ._check_setitem_copy ()
2493
2493
@@ -2503,10 +2503,10 @@ def insert(self, loc, column, value, allow_duplicates=False):
2503
2503
loc : int
2504
2504
Must have 0 <= loc <= len(columns)
2505
2505
column : object
2506
- value : int , Series, or array-like
2506
+ value : scalar , Series, or array-like
2507
2507
"""
2508
2508
self ._ensure_valid_index (value )
2509
- value = self ._sanitize_column (column , value )
2509
+ value = self ._sanitize_column (column , value , broadcast = False )
2510
2510
self ._data .insert (loc , column , value ,
2511
2511
allow_duplicates = allow_duplicates )
2512
2512
@@ -2590,9 +2590,25 @@ def assign(self, **kwargs):
2590
2590
2591
2591
return data
2592
2592
2593
- def _sanitize_column (self , key , value ):
2594
- # Need to make sure new columns (which go into the BlockManager as new
2595
- # blocks) are always copied
2593
+ def _sanitize_column (self , key , value , broadcast = True ):
2594
+ """
2595
+ Ensures new columns (which go into the BlockManager as new blocks) are
2596
+ always copied and converted into an array.
2597
+
2598
+ Parameters
2599
+ ----------
2600
+ key : object
2601
+ value : scalar, Series, or array-like
2602
+ broadcast : bool, default True
2603
+ If ``key`` matches multiple duplicate column names in the
2604
+ DataFrame, this parameter indicates whether ``value`` should be
2605
+ tiled so that the returned array contains a (duplicated) column for
2606
+ each occurrence of the key. If False, ``value`` will not be tiled.
2607
+
2608
+ Returns
2609
+ -------
2610
+ sanitized_column : numpy-array
2611
+ """
2596
2612
2597
2613
def reindexer (value ):
2598
2614
# reindex if necessary
@@ -2665,7 +2681,7 @@ def reindexer(value):
2665
2681
return value
2666
2682
2667
2683
# broadcast across multiple columns if necessary
2668
- if key in self .columns and value .ndim == 1 :
2684
+ if broadcast and key in self .columns and value .ndim == 1 :
2669
2685
if (not self .columns .is_unique or
2670
2686
isinstance (self .columns , MultiIndex )):
2671
2687
existing_piece = self [key ]
0 commit comments