Skip to content

Commit 388e8a9

Browse files
committed
rework
1 parent e83a0b8 commit 388e8a9

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

pandas/_libs/parsers.pyx

+12-10
Original file line numberDiff line numberDiff line change
@@ -1274,23 +1274,25 @@ cdef class TextReader:
12741274
na_hashset, self.c_encoding)
12751275
# sort categories and recode if necessary
12761276
cats = Index(cats)
1277-
if not cats.is_monotonic_increasing:
1277+
if (isinstance(dtype, CategoricalDtype) and
1278+
dtype.categories is not None):
1279+
# redcode for dtype.categories
1280+
categories = dtype.categories
1281+
indexer = cats.get_indexer(categories)
1282+
codes = take_1d(codes, categories, fill_value=-1)
1283+
elif not cats.is_monotonic_increasing:
12781284
unsorted = cats.copy()
12791285
cats = cats.sort_values()
12801286
indexer = cats.get_indexer(unsorted)
12811287
codes = take_1d(indexer, codes, fill_value=-1)
1288+
else:
1289+
categories = cats
12821290

1283-
cat = Categorical(codes, categories=cats, ordered=False,
1291+
cat = Categorical(codes, categories=categories, ordered=False,
12841292
fastpath=True)
12851293

1286-
if isinstance(dtype, CategoricalDtype):
1287-
if dtype.categories is None:
1288-
# skip recoding
1289-
if dtype.ordered:
1290-
cat = cat.set_ordered(ordered=dtype.ordered)
1291-
else:
1292-
cat = cat.set_categories(dtype.categories,
1293-
ordered=dtype.ordered)
1294+
if isinstance(dtype, CategoricalDtype) and dtype.ordered:
1295+
cat = cat.set_ordered(ordered=True)
12941296
return cat, na_count
12951297
elif is_object_dtype(dtype):
12961298
return self._string_convert(i, start, end, na_filter,

0 commit comments

Comments
 (0)