Skip to content

BUG: quantile works for all-nan Series but not all-nan Pint ExtensionArray #45968

Open
@MichaelTiemannOSC

Description

@MichaelTiemannOSC

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
import numpy as np
import pint_pandas
print(pd.Series([1,2,3]).quantile())
# 2.0                                                                                                                                                                                                                                                    
print(pd.Series([np.nan, np.nan, np.nan]).quantile())
# nan                                                                                                                                                                                                                                                    
print(pd.Series([1., np.nan, 3.], dtype='pint[m]').quantile())
# /Users/michael/Documents/GitHub/pint-pandas/pint_pandas/pint_array.py:658: UnitStrippedWarning: The unit of the quantity is stripped when downcasting to ndarray.                                                                                      
#   return np.array(qtys, dtype="object", copy=copy)                                                                                                                                                                                                     
# <Quantity(2.0, 'meter')>                                                                                                                                                                                                                               
print(pd.Series([np.nan, np.nan, np.nan], dtype='pint[m]').quantile())
# ValueError: Cannot infer dtype. No dtype specified and empty array

Issue Description

pd.quantile works as expected for pd.Series of floats, whether some, all, or none of the values are np.nan.

pd.quantile does not work as expected when the pd.Series elements are ExtensionArray elements, such as Pint Quantities. This test case shows it working properly for the case where there's a mixture of np.nan and non-NaN values, but when all values are np.nan it raises a ValueExrror

Expected Behavior

I expect it would return np.nan as the quantile value for an all-nan input under all circumstances.

Installed Versions

I just installed Pandas 1.4.1 this morning (using conda-forge).

No idea why this stopped working:

pd.show_versions()
Traceback (most recent call last):
File "", line 1, in
File "/Users/michael/opt/miniconda3/envs/spyder-env/lib/python3.9/site-packages/pandas/util/_print_versions.py", line 109, in show_versions
deps = _get_dependency_info()
File "/Users/michael/opt/miniconda3/envs/spyder-env/lib/python3.9/site-packages/pandas/util/_print_versions.py", line 88, in _get_dependency_info
mod = import_optional_dependency(modname, errors="ignore")
File "/Users/michael/opt/miniconda3/envs/spyder-env/lib/python3.9/site-packages/pandas/compat/_optional.py", line 126, in import_optional_dependency
module = importlib.import_module(name)
File "/Users/michael/opt/miniconda3/envs/spyder-env/lib/python3.9/importlib/init.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1030, in _gcd_import
File "", line 1007, in _find_and_load
File "", line 986, in _find_and_load_unlocked
File "", line 680, in _load_unlocked
File "", line 850, in exec_module
File "", line 228, in _call_with_frames_removed
File "/Users/michael/opt/miniconda3/envs/spyder-env/lib/python3.9/site-packages/setuptools/init.py", line 8, in
import _distutils_hack.override # noqa: F401
File "/Users/michael/opt/miniconda3/envs/spyder-env/lib/python3.9/site-packages/_distutils_hack/override.py", line 1, in
import('_distutils_hack').do_override()
File "/Users/michael/opt/miniconda3/envs/spyder-env/lib/python3.9/site-packages/_distutils_hack/init.py", line 71, in do_override
ensure_local_distutils()
File "/Users/michael/opt/miniconda3/envs/spyder-env/lib/python3.9/site-packages/_distutils_hack/init.py", line 59, in ensure_local_distutils
assert '_distutils' in core.file, core.file
AssertionError: /Users/michael/opt/miniconda3/envs/spyder-env/lib/python3.9/distutils/core.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugExtensionArrayExtending pandas with custom dtypes or arrays.NA - MaskedArraysRelated to pd.NA and nullable extension arraysquantilequantile method

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions