Skip to content

Commit d79a552

Browse files
committed
Fix mypy
1 parent afe9ca5 commit d79a552

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

doc/source/user_guide/io.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ dtype : Type name or dict of column -> type, default ``None``
198198
the default determines the dtype of the columns which are not explicitly
199199
listed.
200200

201-
use_nullable_dtypes: bool = False
201+
use_nullable_dtypes : bool = False
202202
Whether or not to use nullable dtypes as default when reading data. If
203203
set to True, nullable dtypes are used for all dtypes that have a nullable
204204
implementation, even if no nulls are present.

pandas/core/arrays/categorical.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@
5656
from pandas.util._exceptions import find_stack_level
5757
from pandas.util._validators import validate_bool_kwarg
5858

59-
from pandas.core.dtypes.cast import coerce_indexer_dtype
59+
from pandas.core.dtypes.cast import (
60+
coerce_indexer_dtype,
61+
find_common_type,
62+
)
6063
from pandas.core.dtypes.common import (
6164
ensure_int64,
6265
ensure_platform_int,
@@ -1292,7 +1295,17 @@ def add_categories(
12921295
raise ValueError(
12931296
f"new categories must not include old categories: {already_included}"
12941297
)
1295-
new_categories = list(self.dtype.categories) + list(new_categories)
1298+
1299+
if hasattr(new_categories, "dtype"):
1300+
from pandas import Series
1301+
1302+
dtype = find_common_type([self.categories.dtype, new_categories.dtype])
1303+
new_categories = Series(
1304+
list(self.dtype.categories) + list(new_categories), dtype=dtype
1305+
)
1306+
else:
1307+
new_categories = list(self.dtype.categories) + list(new_categories)
1308+
12961309
new_dtype = CategoricalDtype(new_categories, self.ordered)
12971310

12981311
cat = self if inplace else self.copy()

pandas/io/parsers/base_parser.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
Hashable,
1616
Iterable,
1717
List,
18+
Literal,
1819
Mapping,
1920
Sequence,
2021
Tuple,
@@ -716,7 +717,10 @@ def _infer_types(self, values, na_values, cast_type, try_num_bool: bool = True):
716717
np.putmask(values, mask, np.nan)
717718
return values, na_count
718719

719-
use_nullable_dtypes = self.use_nullable_dtypes and cast_type is None
720+
use_nullable_dtypes: Literal[True] | Literal[False] = (
721+
self.use_nullable_dtypes and cast_type is None
722+
)
723+
result: ArrayLike
720724

721725
if try_num_bool and is_object_dtype(values.dtype):
722726
# exclude e.g DatetimeIndex here
@@ -753,16 +757,16 @@ def _infer_types(self, values, na_values, cast_type, try_num_bool: bool = True):
753757
na_count = parsers.sanitize_objects(values, na_values)
754758

755759
if result.dtype == np.object_ and try_num_bool:
756-
result, mask = libops.maybe_convert_bool(
760+
result, bool_mask = libops.maybe_convert_bool(
757761
np.asarray(values),
758762
true_values=self.true_values,
759763
false_values=self.false_values,
760764
convert_to_masked_nullable=use_nullable_dtypes,
761765
)
762766
if result.dtype == np.bool_ and use_nullable_dtypes:
763-
if mask is None:
764-
mask = np.zeros(result.shape, dtype=np.bool_)
765-
result = BooleanArray(result, mask)
767+
if bool_mask is None:
768+
bool_mask = np.zeros(result.shape, dtype=np.bool_)
769+
result = BooleanArray(result, bool_mask)
766770
elif result.dtype == np.object_ and use_nullable_dtypes:
767771
result = StringDtype().construct_array_type()._from_sequence(values)
768772

pandas/io/parsers/readers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@
427427
428428
.. versionadded:: 1.2
429429
430-
use_nullable_dtypes: bool = False
430+
use_nullable_dtypes : bool = False
431431
Whether or not to use nullable dtypes as default when reading data. If
432432
set to True, nullable dtypes are used for all dtypes that have a nullable
433433
implementation, even if no nulls are present.

0 commit comments

Comments
 (0)