-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
String dtype: implement _get_common_dtype #59682
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
mroeschke
merged 9 commits into
pandas-dev:main
from
jorisvandenbossche:string-dtype-common-type
Sep 6, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0899e4e
String dtype: implement _get_common_dtype
jorisvandenbossche 1bf8a69
add specific tests
jorisvandenbossche 4132ce4
try fix typing
jorisvandenbossche 25f1975
try fix typing
jorisvandenbossche 0fe6a1a
Merge remote-tracking branch 'upstream/main' into string-dtype-common…
jorisvandenbossche 8921a6c
suppress typing error
jorisvandenbossche 641a25a
Merge remote-tracking branch 'upstream/main' into string-dtype-common…
jorisvandenbossche 8f18148
support numpy 2.0 string
jorisvandenbossche be915f7
fix typo
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
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from pandas.compat import HAS_PYARROW | ||
|
||
from pandas.core.dtypes.cast import find_common_type | ||
|
||
import pandas as pd | ||
import pandas._testing as tm | ||
from pandas.util.version import Version | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"to_concat_dtypes, result_dtype", | ||
[ | ||
# same types | ||
([("pyarrow", pd.NA), ("pyarrow", pd.NA)], ("pyarrow", pd.NA)), | ||
([("pyarrow", np.nan), ("pyarrow", np.nan)], ("pyarrow", np.nan)), | ||
([("python", pd.NA), ("python", pd.NA)], ("python", pd.NA)), | ||
([("python", np.nan), ("python", np.nan)], ("python", np.nan)), | ||
# pyarrow preference | ||
([("pyarrow", pd.NA), ("python", pd.NA)], ("pyarrow", pd.NA)), | ||
# NA preference | ||
([("python", pd.NA), ("python", np.nan)], ("python", pd.NA)), | ||
], | ||
) | ||
def test_concat_series(request, to_concat_dtypes, result_dtype): | ||
if any(storage == "pyarrow" for storage, _ in to_concat_dtypes) and not HAS_PYARROW: | ||
pytest.skip("Could not import 'pyarrow'") | ||
|
||
ser_list = [ | ||
pd.Series(["a", "b", None], dtype=pd.StringDtype(storage, na_value)) | ||
for storage, na_value in to_concat_dtypes | ||
] | ||
|
||
result = pd.concat(ser_list, ignore_index=True) | ||
expected = pd.Series( | ||
["a", "b", None, "a", "b", None], dtype=pd.StringDtype(*result_dtype) | ||
) | ||
tm.assert_series_equal(result, expected) | ||
|
||
# order doesn't matter for result | ||
result = pd.concat(ser_list[::1], ignore_index=True) | ||
tm.assert_series_equal(result, expected) | ||
|
||
|
||
def test_concat_with_object(string_dtype_arguments): | ||
# _get_common_dtype cannot inspect values, so object dtype with strings still | ||
# results in object dtype | ||
result = pd.concat( | ||
[ | ||
pd.Series(["a", "b", None], dtype=pd.StringDtype(*string_dtype_arguments)), | ||
pd.Series(["a", "b", None], dtype=object), | ||
] | ||
) | ||
assert result.dtype == np.dtype("object") | ||
|
||
|
||
def test_concat_with_numpy(string_dtype_arguments): | ||
# common type with a numpy string dtype always preserves the pandas string dtype | ||
dtype = pd.StringDtype(*string_dtype_arguments) | ||
assert find_common_type([dtype, np.dtype("U")]) == dtype | ||
assert find_common_type([np.dtype("U"), dtype]) == dtype | ||
assert find_common_type([dtype, np.dtype("U10")]) == dtype | ||
assert find_common_type([np.dtype("U10"), dtype]) == dtype | ||
|
||
# with any other numpy dtype -> object | ||
assert find_common_type([dtype, np.dtype("S")]) == np.dtype("object") | ||
assert find_common_type([dtype, np.dtype("int64")]) == np.dtype("object") | ||
|
||
if Version(np.__version__) >= Version("2"): | ||
assert find_common_type([dtype, np.dtypes.StringDType()]) == dtype | ||
assert find_common_type([np.dtypes.StringDType(), dtype]) == dtype |
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.