Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: support dtypes in column_dtypes for to_records() #24895

Merged
merged 1 commit into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ Reshaping
^^^^^^^^^

- Bug in :func:`merge` when merging by index name would sometimes result in an incorrectly numbered index (:issue:`24212`)
-
- :func:`to_records` now accepts dtypes to its `column_dtypes` parameter (:issue:`24895`)
-


Expand All @@ -213,4 +213,3 @@ Contributors
~~~~~~~~~~~~

.. contributors:: v0.24.x..HEAD

3 changes: 2 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1719,7 +1719,8 @@ def to_records(self, index=True, convert_datetime64=None,
# string naming a type.
if dtype_mapping is None:
formats.append(v.dtype)
elif isinstance(dtype_mapping, (type, compat.string_types)):
elif isinstance(dtype_mapping, (type, np.dtype,
compat.string_types)):
formats.append(dtype_mapping)
else:
element = "row" if i < index_len else "column"
Expand Down
33 changes: 28 additions & 5 deletions pandas/tests/frame/test_convert_to.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

from pandas.compat import long

from pandas import DataFrame, MultiIndex, Series, Timestamp, compat, date_range
from pandas import (
CategoricalDtype, DataFrame, MultiIndex, Series, Timestamp, compat,
date_range)
from pandas.tests.frame.common import TestData
import pandas.util.testing as tm

Expand Down Expand Up @@ -220,6 +222,12 @@ def test_to_records_with_categorical(self):
dtype=[("index", "<i8"), ("A", "<U"),
("B", "<U"), ("C", "<U")])),

# Pass in a dtype instance.
(dict(column_dtypes=np.dtype('unicode')),
np.rec.array([("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
dtype=[("index", "<i8"), ("A", "<U"),
("B", "<U"), ("C", "<U")])),

# Pass in a dictionary (name-only).
(dict(column_dtypes={"A": np.int8, "B": np.float32, "C": "<U2"}),
np.rec.array([("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
Expand Down Expand Up @@ -249,6 +257,12 @@ def test_to_records_with_categorical(self):
dtype=[("index", "<i8"), ("A", "i1"),
("B", "<f4"), ("C", "O")])),

# Names / indices not in dtype mapping default to array dtype.
(dict(column_dtypes={"A": np.dtype('int8'), "B": np.dtype('float32')}),
np.rec.array([("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
dtype=[("index", "<i8"), ("A", "i1"),
("B", "<f4"), ("C", "O")])),

# Mixture of everything.
(dict(column_dtypes={"A": np.int8, "B": np.float32},
index_dtypes="<U2"),
Expand All @@ -258,17 +272,26 @@ def test_to_records_with_categorical(self):

# Invalid dype values.
(dict(index=False, column_dtypes=list()),
"Invalid dtype \\[\\] specified for column A"),
(ValueError, "Invalid dtype \\[\\] specified for column A")),

(dict(index=False, column_dtypes={"A": "int32", "B": 5}),
"Invalid dtype 5 specified for column B"),
(ValueError, "Invalid dtype 5 specified for column B")),

# Numpy can't handle EA types, so check error is raised
(dict(index=False, column_dtypes={"A": "int32",
"B": CategoricalDtype(['a', 'b'])}),
(ValueError, 'Invalid dtype category specified for column B')),

# Check that bad types raise
(dict(index=False, column_dtypes={"A": "int32", "B": "foo"}),
(TypeError, 'data type "foo" not understood')),
])
def test_to_records_dtype(self, kwargs, expected):
# see gh-18146
df = DataFrame({"A": [1, 2], "B": [0.2, 1.5], "C": ["a", "bc"]})

if isinstance(expected, str):
with pytest.raises(ValueError, match=expected):
if not isinstance(expected, np.recarray):
with pytest.raises(expected[0], match=expected[1]):
df.to_records(**kwargs)
else:
result = df.to_records(**kwargs)
Expand Down