Skip to content

CLN: (re-)enable infer_dtype to catch complex #25382

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 3 commits into from
Feb 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,7 @@ _TYPE_MAP = {
'float32': 'floating',
'float64': 'floating',
'f': 'floating',
'complex64': 'complex',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what system actually hits this? AFAIK numpy doesn't use this on a regular basis

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it's as easy to use as float32 on a 64-bit platform. I just added it for completeness.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok see if u can come up with a test then maybe have to force numpy to construct it

'complex128': 'complex',
'c': 'complex',
'string': 'string' if PY2 else 'bytes',
Expand Down Expand Up @@ -1305,6 +1306,9 @@ def infer_dtype(value: object, skipna: object=None) -> str:
elif is_decimal(val):
return 'decimal'

elif is_complex(val):
return 'complex'

elif util.is_float_object(val):
if is_float_array(values):
return 'floating'
Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,30 @@ def test_decimals(self):
result = lib.infer_dtype(arr, skipna=True)
assert result == 'decimal'

def test_complex(self):
# gets cast to complex on array construction
arr = np.array([1.0, 2.0, 1 + 1j])
result = lib.infer_dtype(arr, skipna=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does skipna=False matter anywhere here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because np.nan is compatible with complex. The only thing is that not passing skipna yields a warning.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the parametrize over skipna

assert result == 'complex'

arr = np.array([1.0, 2.0, 1 + 1j], dtype='O')
result = lib.infer_dtype(arr, skipna=True)
assert result == 'mixed'

# gets cast to complex on array construction
arr = np.array([1, np.nan, 1 + 1j])
result = lib.infer_dtype(arr, skipna=True)
assert result == 'complex'

arr = np.array([1.0, np.nan, 1 + 1j], dtype='O')
result = lib.infer_dtype(arr, skipna=True)
assert result == 'mixed'

# complex with nans stays complex
arr = np.array([1 + 1j, np.nan, 3 + 3j], dtype='O')
result = lib.infer_dtype(arr, skipna=True)
assert result == 'complex'

def test_string(self):
pass

Expand Down