Skip to content

COMPAT: float() exception type for py310 #41230

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 2 commits into from
Apr 30, 2021

Conversation

fangchenli
Copy link
Member

@fangchenli fangchenli commented Apr 30, 2021

float() raises TypeError in py310

________________ TestEnsureNumeric.test_non_convertable_values _________________

self = <pandas.tests.test_nanops.TestEnsureNumeric object at 0x7f7da1e029e0>

    def test_non_convertable_values(self):
        msg = "Could not convert foo to numeric"
        with pytest.raises(TypeError, match=msg):
            nanops._ensure_numeric("foo")
    
        # with the wrong type, python raises TypeError for us
        msg = "argument must be a string or a number"
        with pytest.raises(TypeError, match=msg):
>           nanops._ensure_numeric({})

pandas/tests/test_nanops.py:771: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

x = {}

    def _ensure_numeric(x):
        if isinstance(x, np.ndarray):
            if is_integer_dtype(x) or is_bool_dtype(x):
                x = x.astype(np.float64)
            elif is_object_dtype(x):
                try:
                    x = x.astype(np.complex128)
                except (TypeError, ValueError):
                    try:
                        x = x.astype(np.float64)
                    except (TypeError, ValueError) as err:
                        # GH#29941 we get here with object arrays containing strs
                        raise TypeError(f"Could not convert {x} to numeric") from err
                else:
                    if not np.any(np.imag(x)):
                        x = x.real
        elif not (is_float(x) or is_integer(x) or is_complex(x)):
            try:
>               x = float(x)
E               TypeError: float() argument must be a string or a real number, not 'dict'

pandas/core/nanops.py:1600: TypeError

@fangchenli fangchenli added Compat pandas objects compatability with Numpy or Python functions Python 3.10 labels Apr 30, 2021
@jreback jreback added this to the 1.3 milestone Apr 30, 2021
@jreback jreback merged commit afdebd5 into pandas-dev:master Apr 30, 2021
@jreback
Copy link
Contributor

jreback commented Apr 30, 2021

thanks @fangchenli

yeshsurya pushed a commit to yeshsurya/pandas that referenced this pull request May 6, 2021
@fangchenli fangchenli deleted the error-type-compat-py310 branch June 18, 2021 02:29
JulianWgs pushed a commit to JulianWgs/pandas that referenced this pull request Jul 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Compat pandas objects compatability with Numpy or Python functions Python 3.10
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants