Skip to content

Commit b5b06cf

Browse files
ajcrjreback
authored andcommitted
BUG: DataFrame constructor should not promote complex64 dtypes (GH10952)
1 parent 3991731 commit b5b06cf

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

doc/source/whatsnew/v0.17.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,7 @@ Other API Changes
641641
- Allow ``DataFrame`` with ``MultiIndex`` columns to be written to Excel (:issue:`10564`). This was changed in 0.16.2 as the read-back method could not always guarantee perfect fidelity (:issue:`9794`).
642642
- ``groupby`` using ``Categorical`` follows the same rule as ``Categorical.unique`` described above (:issue:`10508`)
643643
- Improved error message when concatenating an empty iterable of dataframes (:issue:`9157`)
644+
- When constructing ``DataFrame`` with an array of ``complex64`` dtype that meant the corresponding column was automatically promoted to the ``complex128`` dtype. Pandas will now preserve the itemsize of the input for complex data (:issue:`10952`)
644645

645646
- ``NaT``'s methods now either raise ``ValueError``, or return ``np.nan`` or ``NaT`` (:issue:`9513`)
646647

@@ -866,4 +867,3 @@ Bug Fixes
866867
- Bug in plotting functions may raise ``IndexError`` when plotted on ``GridSpec`` (:issue:`10819`)
867868
- Bug in plot result may show unnecessary minor ticklabels (:issue:`10657`)
868869
- Bug in ``groupby`` incorrect computation for aggregation on ``DataFrame`` with ``NaT`` (E.g ``first``, ``last``, ``min``). (:issue:`10590`)
869-
- Bug when constructing ``DataFrame`` where passing a dictionary with only scalar values and specifying columns did not raise an error (:issue:`10856`)

pandas/core/internals.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -3657,8 +3657,7 @@ def form_blocks(arrays, names, axes):
36573657
blocks.extend(float_blocks)
36583658

36593659
if len(complex_items):
3660-
complex_blocks = _simple_blockify(
3661-
complex_items, np.complex128)
3660+
complex_blocks = _multi_blockify(complex_items)
36623661
blocks.extend(complex_blocks)
36633662

36643663
if len(int_items):

pandas/tests/test_frame.py

+9
Original file line numberDiff line numberDiff line change
@@ -2668,6 +2668,15 @@ def _check_mixed_dtypes(df, dtypes = None):
26682668
df = _make_mixed_dtypes_df('int')
26692669
_check_mixed_dtypes(df)
26702670

2671+
def test_constructor_complex_dtypes(self):
2672+
# GH10952
2673+
a = np.random.rand(10).astype(np.complex64)
2674+
b = np.random.rand(10).astype(np.complex128)
2675+
2676+
df = DataFrame({'a': a, 'b': b})
2677+
self.assertEqual(a.dtype, df.a.dtype)
2678+
self.assertEqual(b.dtype, df.b.dtype)
2679+
26712680
def test_constructor_rec(self):
26722681
rec = self.frame.to_records(index=False)
26732682

0 commit comments

Comments
 (0)