Skip to content

Commit b80cff8

Browse files
committed
More coercion, use _recode_for_categories
1 parent de9e3ee commit b80cff8

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

pandas/_libs/parsers.pyx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ from pandas.core.dtypes.common import (
4545
is_bool_dtype, is_object_dtype,
4646
is_string_dtype, is_datetime64_dtype,
4747
pandas_dtype)
48-
from pandas.core.categorical import Categorical
48+
from pandas.core.categorical import Categorical, _recode_for_categories
4949
from pandas.core.algorithms import take_1d
5050
from pandas.core.dtypes.concat import union_categoricals
5151
from pandas import Index, to_numeric, to_datetime, to_timedelta
@@ -1292,8 +1292,7 @@ cdef class TextReader:
12921292
dtype.categories is not None):
12931293
# recode for dtype.categories
12941294
categories = dtype.categories
1295-
indexer = categories.get_indexer(cats)
1296-
codes = take_1d(indexer, codes, fill_value=-1)
1295+
codes = _recode_for_categories(codes, cats, categories)
12971296
ordered = dtype.ordered
12981297
elif not cats.is_monotonic_increasing:
12991298
# sort categories and recode if necessary

pandas/tests/io/parser/dtypes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,14 @@ def test_categoricaldtype_coerces_datetime(self):
203203
result = self.read_csv(StringIO(data), dtype=dtype)
204204
tm.assert_frame_equal(result, expected)
205205

206+
dtype = {
207+
'b': CategoricalDtype([pd.Timestamp("2014")])
208+
}
209+
data = "b\n2014-01-01\n2014-01-01T00:00:00"
210+
expected = pd.DataFrame({'b': Categorical([pd.Timestamp('2014')] * 2)})
211+
result = self.read_csv(StringIO(data), dtype=dtype)
212+
tm.assert_frame_equal(result, expected)
213+
206214
def test_categoricaldtype_coerces_timedelta(self):
207215
dtype = {'b': CategoricalDtype(pd.to_timedelta(['1H', '2H', '3H']))}
208216
data = "b\n1H\n2H\n3H"

0 commit comments

Comments
 (0)