-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: include conversion to nullable float in convert_dtypes() #38117
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
jreback
merged 10 commits into
pandas-dev:master
from
jorisvandenbossche:floating-convert-dtypes
Nov 29, 2020
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
90bcb4d
add float to convert_dtypes
jorisvandenbossche 96b9b07
fix convert_floating
jorisvandenbossche bf87361
TST: rewrite convert_dtypes test to make it easier extendable
jorisvandenbossche e2e6bdc
formatting
jorisvandenbossche e411e65
update tests
jorisvandenbossche 031acaa
fix implementation for integer-like floats
jorisvandenbossche bf95155
update docs
jorisvandenbossche 3106644
test + fix case of float32 as input
jorisvandenbossche 80f8ee9
Merge remote-tracking branch 'upstream/master' into floating-convert-…
jorisvandenbossche 693a0a4
add note about changed behaviour
jorisvandenbossche File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6088,6 +6088,7 @@ def convert_dtypes( | |
convert_string: bool_t = True, | ||
convert_integer: bool_t = True, | ||
convert_boolean: bool_t = True, | ||
convert_floating: bool_t = True, | ||
) -> FrameOrSeries: | ||
""" | ||
Convert columns to best possible dtypes using dtypes supporting ``pd.NA``. | ||
|
@@ -6104,6 +6105,12 @@ def convert_dtypes( | |
Whether, if possible, conversion can be done to integer extension types. | ||
convert_boolean : bool, defaults True | ||
Whether object dtypes should be converted to ``BooleanDtypes()``. | ||
convert_floating : bool, defaults True | ||
Whether, if possible, conversion can be done to floating extension types. | ||
If `convert_integer` is also True, preference will be give to integer | ||
dtypes if the floats can be faithfully casted to integers. | ||
|
||
.. versionadded:: 1.2.0 | ||
|
||
Returns | ||
------- | ||
|
@@ -6121,19 +6128,25 @@ def convert_dtypes( | |
----- | ||
By default, ``convert_dtypes`` will attempt to convert a Series (or each | ||
Series in a DataFrame) to dtypes that support ``pd.NA``. By using the options | ||
``convert_string``, ``convert_integer``, and ``convert_boolean``, it is | ||
possible to turn off individual conversions to ``StringDtype``, the integer | ||
extension types or ``BooleanDtype``, respectively. | ||
``convert_string``, ``convert_integer``, ``convert_boolean`` and | ||
``convert_boolean``, it is possible to turn off individual conversions | ||
to ``StringDtype``, the integer extension types, ``BooleanDtype`` | ||
or floating extension types, respectively. | ||
|
||
For object-dtyped columns, if ``infer_objects`` is ``True``, use the inference | ||
rules as during normal Series/DataFrame construction. Then, if possible, | ||
convert to ``StringDtype``, ``BooleanDtype`` or an appropriate integer extension | ||
type, otherwise leave as ``object``. | ||
convert to ``StringDtype``, ``BooleanDtype`` or an appropriate integer | ||
or floating extension type, otherwise leave as ``object``. | ||
|
||
If the dtype is integer, convert to an appropriate integer extension type. | ||
|
||
If the dtype is numeric, and consists of all integers, convert to an | ||
appropriate integer extension type. | ||
appropriate integer extension type. Otherwise, convert to an | ||
appropriate floating extension type. | ||
|
||
.. versionchanged:: 1.2 | ||
Starting with pandas 1.2, this method also converts float columns | ||
to the nullable floating extension type. | ||
|
||
In the future, as new dtypes are added that support ``pd.NA``, the results | ||
of this method will change to support those new dtypes. | ||
|
@@ -6173,7 +6186,7 @@ def convert_dtypes( | |
>>> dfn = df.convert_dtypes() | ||
>>> dfn | ||
a b c d e f | ||
0 1 x True h 10 NaN | ||
0 1 x True h 10 <NA> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. might be worth mentioning here that this changed in 1.2 |
||
1 2 y False i <NA> 100.5 | ||
2 3 z <NA> <NA> 20 200.0 | ||
|
||
|
@@ -6183,7 +6196,7 @@ def convert_dtypes( | |
c boolean | ||
d string | ||
e Int64 | ||
f float64 | ||
f Float64 | ||
dtype: object | ||
|
||
Start with a Series of strings and missing data represented by ``np.nan``. | ||
|
@@ -6205,12 +6218,20 @@ def convert_dtypes( | |
""" | ||
if self.ndim == 1: | ||
return self._convert_dtypes( | ||
infer_objects, convert_string, convert_integer, convert_boolean | ||
infer_objects, | ||
convert_string, | ||
convert_integer, | ||
convert_boolean, | ||
convert_floating, | ||
) | ||
else: | ||
results = [ | ||
col._convert_dtypes( | ||
infer_objects, convert_string, convert_integer, convert_boolean | ||
infer_objects, | ||
convert_string, | ||
convert_integer, | ||
convert_boolean, | ||
convert_floating, | ||
) | ||
for col_name, col in self.items() | ||
] | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.