Skip to content

BUG: Fix bug, where BooleanDtype columns are converted to Int64 #32490

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

Merged
merged 8 commits into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Bug fixes
- Fixed bug in setting values using a slice indexer with string dtype (:issue:`31772`)
- 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`)
- Fix bug in :meth:`Series.convert_dtypes` for series with mix of integers and strings (:issue:`32117`)
- Fixed bug in :meth:`DataFrame.convert_dtypes`, where ``BooleanDtype`` columns were converted to ``Int64`` (:issue:`32287`)

.. ---------------------------------------------------------------------------

Expand Down
7 changes: 5 additions & 2 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
from pandas.core.dtypes.inference import is_list_like
from pandas.core.dtypes.missing import isna, notna

import pandas as pd

_int8_max = np.iinfo(np.int8).max
_int16_max = np.iinfo(np.int16).max
_int32_max = np.iinfo(np.int32).max
Expand Down Expand Up @@ -1078,8 +1080,9 @@ def convert_dtypes(
inferred_dtype = input_array.dtype

if convert_boolean:
if is_bool_dtype(input_array.dtype) and not is_extension_array_dtype(
input_array.dtype
if is_bool_dtype(input_array.dtype) and (
(not is_extension_array_dtype(input_array.dtype))
or (input_array.dtype == pd.BooleanDtype())
):
inferred_dtype = "boolean"
else:
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/series/methods/test_convert_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,16 @@ def test_convert_string_dtype(self):
)
result = df.convert_dtypes()
tm.assert_frame_equal(df, result)

def test_convert_bool_dtype(self):
# GH32287
df = pd.DataFrame([["abc", 123, True]])
exp_dtypes = pd.Series([np.object, np.int64, np.bool])
tm.assert_series_equal(df.dtypes, exp_dtypes)

df_1 = df.convert_dtypes()
exp_dtypes_1 = pd.Series([pd.StringDtype(), pd.Int64Dtype(), pd.BooleanDtype()])
tm.assert_series_equal(df_1.dtypes, exp_dtypes_1)

df_2 = df_1.convert_dtypes()
tm.assert_series_equal(df_2.dtypes, exp_dtypes_1)