Skip to content

Commit 7e48531

Browse files
committed
Fixed bug, where BooleanDtype columns were converted to Int64
1 parent 4018550 commit 7e48531

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

doc/source/whatsnew/v1.0.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Bug fixes
8383
- Fixed bug in setting values using a slice indexer with string dtype (:issue:`31772`)
8484
- Fixed bug where :meth:`GroupBy.first` and :meth:`GroupBy.last` would raise a ``TypeError`` when groups contained ``pd.NA`` in a column of object dtype (:issue:`32123`)
8585
- Fix bug in :meth:`Series.convert_dtypes` for series with mix of integers and strings (:issue:`32117`)
86+
- Fixed bug in :meth:`DataFrame.convert_dtypes`, where ``BooleanDtype`` columns were converted to ``Int64`` (:issue:`32287`)
8687

8788
.. ---------------------------------------------------------------------------
8889

pandas/core/dtypes/cast.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import numpy as np
88

9+
import pandas as pd
910
from pandas._libs import lib, tslib, tslibs
1011
from pandas._libs.tslibs import (
1112
NaT,
@@ -1078,8 +1079,9 @@ def convert_dtypes(
10781079
inferred_dtype = input_array.dtype
10791080

10801081
if convert_boolean:
1081-
if is_bool_dtype(input_array.dtype) and not is_extension_array_dtype(
1082-
input_array.dtype
1082+
if is_bool_dtype(input_array.dtype) and (
1083+
(not is_extension_array_dtype(input_array.dtype))
1084+
or (input_array.dtype == pd.BooleanDtype())
10831085
):
10841086
inferred_dtype = "boolean"
10851087
else:

pandas/tests/series/methods/test_convert_dtypes.py

+13
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,16 @@ def test_convert_string_dtype(self):
279279
)
280280
result = df.convert_dtypes()
281281
tm.assert_frame_equal(df, result)
282+
283+
def test_convert_bool_dtype(self):
284+
# GH32287
285+
df = pd.DataFrame([["abc", 123, True]])
286+
exp_dtypes = pd.Series([np.object, np.int64, np.bool])
287+
tm.assert_series_equal(df.dtypes, exp_dtypes)
288+
289+
df_1 = df.convert_dtypes()
290+
exp_dtypes_1 = pd.Series([pd.StringDtype(), pd.Int64Dtype(), pd.BooleanDtype()])
291+
tm.assert_series_equal(df_1.dtypes, exp_dtypes_1)
292+
293+
df_2 = df_1.convert_dtypes()
294+
tm.assert_series_equal(df_2.dtypes, exp_dtypes_1)

0 commit comments

Comments
 (0)