-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Accept CategoricalDtype in read_csv #17643
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
Changes from 6 commits
e83a0b8
388e8a9
c5f6e04
4b588cd
e32d5be
508dd1e
6f175a7
1545734
de9e3ee
b80cff8
b028827
fc34080
d100f0c
8600c50
8c4ab5b
96d5144
3de75cd
f03798d
9325a93
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -452,7 +452,8 @@ Specifying Categorical dtype | |
|
||
.. versionadded:: 0.19.0 | ||
|
||
``Categorical`` columns can be parsed directly by specifying ``dtype='category'`` | ||
``Categorical`` columns can be parsed directly by specifying ``dtype='category'`` or | ||
``dtype=CategoricalDtype(categories, ordered)``. | ||
|
||
.. ipython:: python | ||
|
||
|
@@ -468,12 +469,28 @@ Individual columns can be parsed as a ``Categorical`` using a dict specification | |
|
||
pd.read_csv(StringIO(data), dtype={'col1': 'category'}).dtypes | ||
|
||
Specifying ``dtype='cateogry'`` will result in an unordered ``Categorical`` | ||
whose ``categories`` are the unique values observed in the data. For more | ||
control on the categories and order, create a | ||
:class:`~pandas.api.types.CategoricalDtype` ahead of time, and pass that for | ||
that column's ``dtype``. | ||
|
||
.. ipython:: python | ||
|
||
from pandas.api.types import CategoricalDtype | ||
|
||
dtype = CategoricalDtype(['d', 'c', 'b', 'a'], ordered=True) | ||
pd.read_csv(StringIO(data), dtype={'col1': dtype}).dtypes | ||
|
||
.. note:: | ||
|
||
The resulting categories will always be parsed as strings (object dtype). | ||
If the categories are numeric they can be converted using the | ||
:func:`to_numeric` function, or as appropriate, another converter | ||
such as :func:`to_datetime`. | ||
With ``dtype='category'``, the resulting categories will always be parsed | ||
as strings (object dtype). If the categories are numeric they can be | ||
converted using the :func:`to_numeric` function, or as appropriate, another | ||
converter such as :func:`to_datetime`. | ||
|
||
When ``dtype`` is a ``CategoricalDtype`` with homogenous ``categoriess`` ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. categoriess -> categories |
||
all numeric, all datetimes, etc.), the conversion is done automatically. | ||
|
||
.. ipython:: python | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -163,6 +163,8 @@ Other Enhancements | |
- :func:`Categorical.rename_categories` now accepts a dict-like argument as `new_categories` and only updates the categories found in that dict. (:issue:`17336`) | ||
- :func:`read_excel` raises ``ImportError`` with a better message if ``xlrd`` is not installed. (:issue:`17613`) | ||
- :meth:`DataFrame.assign` will preserve the original order of ``**kwargs`` for Python 3.6+ users instead of sorting the column names | ||
- Pass a :class:`~pandas.api.types.CategoricalDtype` to :meth:`read_csv` to parse categorical | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would clarify this should be passed to the dtype keyword? Also, apart from the fact you can also have non-string categories, are there not more benefits (like being able to specify the categories yourself, specific order, ... performance?) ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps I'll merge this with the main section for CategoricalDtype. (no extra performance yet though) |
||
data as numeric, datetimes, or timedeltas, instead of strings. See :ref:`here <io.categorical>`. (:issue:`17643`) | ||
|
||
|
||
.. _whatsnew_0210.api_breaking: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,7 @@ from pandas.core.dtypes.common import ( | |
from pandas.core.categorical import Categorical | ||
from pandas.core.algorithms import take_1d | ||
from pandas.core.dtypes.concat import union_categoricals | ||
from pandas import Index | ||
from pandas import Index, to_numeric, to_datetime, to_timedelta | ||
|
||
import pandas.io.common as com | ||
|
||
|
@@ -1267,19 +1267,49 @@ cdef class TextReader: | |
return self._string_convert(i, start, end, na_filter, | ||
na_hashset) | ||
elif is_categorical_dtype(dtype): | ||
# TODO: I suspect that _categorical_convert could be | ||
# optimized when dtype is an instance of CategoricalDtype | ||
codes, cats, na_count = _categorical_convert( | ||
self.parser, i, start, end, na_filter, | ||
na_hashset, self.c_encoding) | ||
# sort categories and recode if necessary | ||
cats = Index(cats) | ||
if not cats.is_monotonic_increasing: | ||
|
||
# Determine if we should convert inferred string | ||
# categories to a specialized type | ||
if (isinstance(dtype, CategoricalDtype) and | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would rather move this entire section to a free function (except for the actual constructor) maybe
NONE of this logic should be here |
||
dtype.categories is not None): | ||
if dtype.categories.is_numeric(): | ||
# is ignore correct? | ||
cats = to_numeric(cats, errors='ignore') | ||
elif dtype.categories.is_all_dates: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this may leave open corner cases where strings don't map 1->1 with categories? For example:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I don't follow. This passes: dtype = {
'b': CategoricalDtype([pd.Timestamp("2014")])
}
# Two representations of the same value
data = "b\n2014-01-01\n2014-01-01T00:00:00"
expected = pd.DataFrame({'b': Categorical([pd.Timestamp('2014')] * 2)})
result = self.read_csv(StringIO(data), dtype=dtype)
tm.assert_frame_equal(result, expected) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No problem. It has multiple values, but the categories are unique. In [10]: pd.read_csv(StringIO(data), dtype=dtype).b.dtype
Out[10]: CategoricalDtype(categories=['2014-01-01'], ordered=False) The |
||
# is ignore correct? | ||
if is_datetime64_dtype(dtype.categories): | ||
cats = to_datetime(cats, errors='ignore') | ||
else: | ||
cats = to_timedelta(cats, errors='ignore') | ||
|
||
if (isinstance(dtype, CategoricalDtype) and | ||
dtype.categories is not None): | ||
# recode for dtype.categories | ||
categories = dtype.categories | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed (will wait to push until I hear back about #17643 (comment)) |
||
indexer = categories.get_indexer(cats) | ||
codes = take_1d(indexer, codes, fill_value=-1) | ||
ordered = dtype.ordered | ||
elif not cats.is_monotonic_increasing: | ||
# sort categories and recode if necessary | ||
unsorted = cats.copy() | ||
cats = cats.sort_values() | ||
indexer = cats.get_indexer(unsorted) | ||
categories = cats.sort_values() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would move ALL of this logic and simply create a new factory for |
||
indexer = categories.get_indexer(unsorted) | ||
codes = take_1d(indexer, codes, fill_value=-1) | ||
ordered = False | ||
else: | ||
categories = cats | ||
ordered = False | ||
|
||
cat = Categorical(codes, categories=categories, ordered=ordered, | ||
fastpath=True) | ||
|
||
return Categorical(codes, categories=cats, ordered=False, | ||
fastpath=True), na_count | ||
return cat, na_count | ||
elif is_object_dtype(dtype): | ||
return self._string_convert(i, start, end, na_filter, | ||
na_hashset) | ||
|
@@ -2230,8 +2260,11 @@ def _concatenate_chunks(list chunks): | |
if common_type == np.object: | ||
warning_columns.append(str(name)) | ||
|
||
if is_categorical_dtype(dtypes.pop()): | ||
result[name] = union_categoricals(arrs, sort_categories=True) | ||
dtype = dtypes.pop() | ||
if is_categorical_dtype(dtype): | ||
sort_categories = isinstance(dtype, str) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. str -> string_types |
||
result[name] = union_categoricals(arrs, | ||
sort_categories=sort_categories) | ||
else: | ||
result[name] = np.concatenate(arrs) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,15 +12,17 @@ | |
|
||
import numpy as np | ||
|
||
from pandas import compat | ||
from pandas import compat, to_numeric, to_timedelta | ||
from pandas.compat import (range, lrange, PY3, StringIO, lzip, | ||
zip, string_types, map, u) | ||
from pandas.core.dtypes.common import ( | ||
is_integer, _ensure_object, | ||
is_list_like, is_integer_dtype, | ||
is_float, is_dtype_equal, | ||
is_object_dtype, is_string_dtype, | ||
is_scalar, is_categorical_dtype) | ||
is_scalar, is_categorical_dtype, | ||
is_datetime64_dtype, is_timedelta64_dtype) | ||
from pandas.core.dtypes.dtypes import CategoricalDtype | ||
from pandas.core.dtypes.missing import isna | ||
from pandas.core.dtypes.cast import astype_nansafe | ||
from pandas.core.index import (Index, MultiIndex, RangeIndex, | ||
|
@@ -1605,9 +1607,23 @@ def _cast_types(self, values, cast_type, column): | |
# XXX this is for consistency with | ||
# c-parser which parses all categories | ||
# as strings | ||
if not is_object_dtype(values): | ||
known_cats = (isinstance(cast_type, CategoricalDtype) and | ||
cast_type.categories is not None) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. none of this logic should live here either. move to pandas.core.dtypes.cast.py (also ok with a new module pandas.core.dtypes.categorical.py if its simpler) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactored most of this to |
||
str_values = is_object_dtype(values) | ||
|
||
if known_cats and str_values: | ||
if cast_type.categories.is_numeric(): | ||
values = to_numeric(values, errors='ignore') | ||
elif is_datetime64_dtype(cast_type.categories): | ||
values = tools.to_datetime(values, errors='ignore') | ||
elif is_timedelta64_dtype(cast_type.categories): | ||
values = to_timedelta(values, errors='ignore') | ||
values = Categorical(values, categories=cast_type.categories, | ||
ordered=cast_type.ordered) | ||
elif not is_object_dtype(values): | ||
values = astype_nansafe(values, str) | ||
values = Categorical(values) | ||
else: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any reason you are not handling this case as well? (I get that it conflates the purpose of I don't like to scatter casting/inferrence code around, very hard to figure out what's going on when when its not in 1 place. |
||
values = Categorical(values) | ||
else: | ||
try: | ||
values = astype_nansafe(values, cast_type, copy=True) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
versionadded here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe a sub-section for this?