40
40
)
41
41
from pandas .core .internals .blocks import (
42
42
ensure_block_shape ,
43
+ maybe_coerce_values ,
43
44
new_block ,
44
45
)
45
46
@@ -168,6 +169,7 @@ def create_block(typestr, placement, item_shape=None, num_offset=0, maker=new_bl
168
169
else :
169
170
raise ValueError (f'Unsupported typestr: "{ typestr } "' )
170
171
172
+ values = maybe_coerce_values (values )
171
173
return maker (values , placement = placement , ndim = len (shape ))
172
174
173
175
@@ -349,7 +351,7 @@ def test_delete_datetimelike(self):
349
351
def test_split (self ):
350
352
# GH#37799
351
353
values = np .random .randn (3 , 4 )
352
- blk = new_block (values , placement = [3 , 1 , 6 ], ndim = 2 )
354
+ blk = new_block (values , placement = BlockPlacement ( [3 , 1 , 6 ]) , ndim = 2 )
353
355
result = blk ._split ()
354
356
355
357
# check that we get views, not copies
@@ -358,9 +360,9 @@ def test_split(self):
358
360
359
361
assert len (result ) == 3
360
362
expected = [
361
- new_block (values [[0 ]], placement = [3 ], ndim = 2 ),
362
- new_block (values [[1 ]], placement = [1 ], ndim = 2 ),
363
- new_block (values [[2 ]], placement = [6 ], ndim = 2 ),
363
+ new_block (values [[0 ]], placement = BlockPlacement ( [3 ]) , ndim = 2 ),
364
+ new_block (values [[1 ]], placement = BlockPlacement ( [1 ]) , ndim = 2 ),
365
+ new_block (values [[2 ]], placement = BlockPlacement ( [6 ]) , ndim = 2 ),
364
366
]
365
367
for res , exp in zip (result , expected ):
366
368
assert_block_equal (res , exp )
@@ -425,7 +427,7 @@ def test_iget(self):
425
427
values = np .random .rand (3 , 3 )
426
428
block = new_block (
427
429
values = values .copy (),
428
- placement = np .arange (3 , dtype = np .intp ),
430
+ placement = BlockPlacement ( np .arange (3 , dtype = np .intp ) ),
429
431
ndim = values .ndim ,
430
432
)
431
433
mgr = BlockManager (blocks = (block ,), axes = [cols , Index (np .arange (3 ))])
@@ -629,13 +631,6 @@ def _compare(old_mgr, new_mgr):
629
631
assert new_mgr .iget (7 ).dtype == np .float64
630
632
assert new_mgr .iget (8 ).dtype == np .float16
631
633
632
- def test_invalid_ea_block (self ):
633
- with pytest .raises (ValueError , match = "need to split" ):
634
- create_mgr ("a: category; b: category" )
635
-
636
- with pytest .raises (ValueError , match = "need to split" ):
637
- create_mgr ("a: category2; b: category2" )
638
-
639
634
def test_interleave (self ):
640
635
# self
641
636
for dtype in ["f8" , "i8" , "object" , "bool" , "complex" , "M8[ns]" , "m8[ns]" ]:
@@ -1301,7 +1296,7 @@ def test_datetime_block_can_hold_element(self):
1301
1296
def test_interval_can_hold_element_emptylist (self , dtype , element ):
1302
1297
arr = np .array ([1 , 3 , 4 ], dtype = dtype )
1303
1298
ii = IntervalIndex .from_breaks (arr )
1304
- blk = new_block (ii ._data , [1 ], ndim = 2 )
1299
+ blk = new_block (ii ._data , BlockPlacement ( [1 ]) , ndim = 2 )
1305
1300
1306
1301
assert blk ._can_hold_element ([])
1307
1302
# TODO: check this holds for all blocks
@@ -1310,7 +1305,7 @@ def test_interval_can_hold_element_emptylist(self, dtype, element):
1310
1305
def test_interval_can_hold_element (self , dtype , element ):
1311
1306
arr = np .array ([1 , 3 , 4 , 9 ], dtype = dtype )
1312
1307
ii = IntervalIndex .from_breaks (arr )
1313
- blk = new_block (ii ._data , [1 ], ndim = 2 )
1308
+ blk = new_block (ii ._data , BlockPlacement ( [1 ]) , ndim = 2 )
1314
1309
1315
1310
elem = element (ii )
1316
1311
self .check_series_setitem (elem , ii , True )
@@ -1335,7 +1330,7 @@ def test_interval_can_hold_element(self, dtype, element):
1335
1330
1336
1331
def test_period_can_hold_element_emptylist (self ):
1337
1332
pi = period_range ("2016" , periods = 3 , freq = "A" )
1338
- blk = new_block (pi ._data .reshape (1 , 3 ), [1 ], ndim = 2 )
1333
+ blk = new_block (pi ._data .reshape (1 , 3 ), BlockPlacement ( [1 ]) , ndim = 2 )
1339
1334
1340
1335
assert blk ._can_hold_element ([])
1341
1336
@@ -1396,13 +1391,13 @@ def test_should_store_categorical(self):
1396
1391
assert not blk .should_store (np .asarray (cat ))
1397
1392
1398
1393
1399
- def test_validate_ndim (block_maker ):
1394
+ def test_validate_ndim ():
1400
1395
values = np .array ([1.0 , 2.0 ])
1401
- placement = slice (2 )
1396
+ placement = BlockPlacement ( slice (2 ) )
1402
1397
msg = r"Wrong number of dimensions. values.ndim != ndim \[1 != 2\]"
1403
1398
1404
1399
with pytest .raises (ValueError , match = msg ):
1405
- block_maker (values , placement , ndim = 2 )
1400
+ make_block (values , placement , ndim = 2 )
1406
1401
1407
1402
1408
1403
def test_block_shape ():
@@ -1418,7 +1413,7 @@ def test_make_block_no_pandas_array(block_maker):
1418
1413
arr = pd .arrays .PandasArray (np .array ([1 , 2 ]))
1419
1414
1420
1415
# PandasArray, no dtype
1421
- result = block_maker (arr , slice (len (arr )), ndim = arr .ndim )
1416
+ result = block_maker (arr , BlockPlacement ( slice (len (arr ) )), ndim = arr .ndim )
1422
1417
assert result .dtype .kind in ["i" , "u" ]
1423
1418
1424
1419
if block_maker is make_block :
0 commit comments