-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG/TST: Fix infer_dtype for Period array-likes and general ExtensionArrays #37367
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
Changes from all commits
a3b7ad7
6aa4475
d6f6c0a
50d7425
ed36be2
ce64b64
0e15a9a
688104f
f5ea183
4c539e3
0504ad1
88dcd81
b53f46e
100b83b
fbb713d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,6 +69,7 @@ from pandas._libs cimport util | |
from pandas._libs.util cimport INT64_MAX, INT64_MIN, UINT64_MAX, is_nan | ||
|
||
from pandas._libs.tslib import array_to_datetime | ||
from pandas._libs.tslibs.period import Period | ||
|
||
from pandas._libs.missing cimport ( | ||
C_NA, | ||
|
@@ -1082,6 +1083,7 @@ _TYPE_MAP = { | |
"timedelta64[ns]": "timedelta64", | ||
"m": "timedelta64", | ||
"interval": "interval", | ||
Period: "period", | ||
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. prob need Interval here? 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. Interval is already handled by the "interval" string in the line above, see the non-inline comment with the link to PR were this was fixed before |
||
} | ||
|
||
# types only exist on certain platform | ||
|
@@ -1233,8 +1235,8 @@ cdef object _try_infer_map(object dtype): | |
cdef: | ||
object val | ||
str attr | ||
for attr in ["name", "kind", "base"]: | ||
val = getattr(dtype, attr) | ||
for attr in ["name", "kind", "base", "type"]: | ||
val = getattr(dtype, attr, None) | ||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if val in _TYPE_MAP: | ||
return _TYPE_MAP[val] | ||
return None | ||
|
@@ -1275,6 +1277,7 @@ def infer_dtype(value: object, skipna: bool = True) -> str: | |
- time | ||
- period | ||
- mixed | ||
- unknown-array | ||
|
||
Raises | ||
------ | ||
|
@@ -1287,6 +1290,9 @@ def infer_dtype(value: object, skipna: bool = True) -> str: | |
specialized | ||
- 'mixed-integer-float' are floats and integers | ||
- 'mixed-integer' are integers mixed with non-integers | ||
- 'unknown-array' is the catchall for something that *is* an array (has | ||
a dtype attribute), but has a dtype unknown to pandas (e.g. external | ||
extension array) | ||
|
||
Examples | ||
-------- | ||
|
@@ -1355,12 +1361,10 @@ def infer_dtype(value: object, skipna: bool = True) -> str: | |
# e.g. categoricals | ||
dtype = value.dtype | ||
if not isinstance(dtype, np.dtype): | ||
value = _try_infer_map(value.dtype) | ||
if value is not None: | ||
return value | ||
|
||
# its ndarray-like but we can't handle | ||
raise ValueError(f"cannot infer type for {type(value)}") | ||
inferred = _try_infer_map(value.dtype) | ||
if inferred is not None: | ||
return inferred | ||
return "unknown-array" | ||
|
||
# Unwrap Series/Index | ||
values = np.asarray(value) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -202,11 +202,7 @@ def _validate(data): | |
if isinstance(values.dtype, StringDtype): | ||
return "string" | ||
|
||
try: | ||
inferred_dtype = lib.infer_dtype(values, skipna=True) | ||
except ValueError: | ||
# GH#27571 mostly occurs with ExtensionArray | ||
inferred_dtype = None | ||
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. there's another case almost identical to this in dtypes.cast.convert_dtypes |
||
inferred_dtype = lib.infer_dtype(values, skipna=True) | ||
|
||
if inferred_dtype not in allowed_types: | ||
raise AttributeError("Can only use .str accessor with string values!") | ||
|
Uh oh!
There was an error while loading. Please reload this page.