Skip to content
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

[Python] pandas is_sparse attribute is deprecated #35329

Closed
jorisvandenbossche opened this issue Apr 25, 2023 · 0 comments · Fixed by #35366
Closed

[Python] pandas is_sparse attribute is deprecated #35329

jorisvandenbossche opened this issue Apr 25, 2023 · 0 comments · Fixed by #35366

Comments

@jorisvandenbossche
Copy link
Member

We see warnings in the nightly logs:

 opt/conda/envs/arrow/lib/python3.9/site-packages/pyarrow/tests/parquet/test_metadata.py: 13 warnings
opt/conda/envs/arrow/lib/python3.9/site-packages/pyarrow/tests/parquet/test_pandas.py: 4 warnings
  /opt/conda/envs/arrow/lib/python3.9/site-packages/pyarrow/pandas_compat.py:456: FutureWarning: is_sparse is deprecated and will be removed in a future version. Check `isinstance(dtype, pd.SparseDtype)` instead.
    if _pandas_api.is_sparse(col):

and that also causes two actual test failures because we are ensuring some code didn't trigger any warning

 _______________ TestConvertMetadata.test_rangeindex_doesnt_warn ________________

self = <pyarrow.tests.test_pandas.TestConvertMetadata object at 0x7f0969e30b40>

    def test_rangeindex_doesnt_warn(self):
        # ARROW-5606: pandas 0.25 deprecated private _start/stop/step
        # attributes -> can be removed if support < pd 0.25 is dropped
        df = pd.DataFrame(np.random.randn(4, 2), columns=['a', 'b'])
    
        with warnings.catch_warnings():
            warnings.simplefilter(action="error")
>           _check_pandas_roundtrip(df, preserve_index=True)

opt/conda/envs/arrow/lib/python3.9/site-packages/pyarrow/tests/test_pandas.py:247: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
opt/conda/envs/arrow/lib/python3.9/site-packages/pyarrow/tests/test_pandas.py:86: in _check_pandas_roundtrip
    table = klass.from_pandas(df, schema=schema,
pyarrow/table.pxi:3681: in pyarrow.lib.Table.from_pandas
    ???
opt/conda/envs/arrow/lib/python3.9/site-packages/pyarrow/pandas_compat.py:570: in dataframe_to_arrays
    convert_fields) = _get_columns_to_convert(df, schema, preserve_index,
opt/conda/envs/arrow/lib/python3.9/site-packages/pyarrow/pandas_compat.py:373: in _get_columns_to_convert
    if _pandas_api.is_sparse(col):
pyarrow/pandas-shim.pxi:191: in pyarrow.lib._PandasAPIShim.is_sparse
    ???
pyarrow/pandas-shim.pxi:193: in pyarrow.lib._PandasAPIShim.is_sparse
    ???
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

arr = 0   -0.299318
1    0.115113
2    2.195990
3    1.032101
Name: a, dtype: float64

    def is_sparse(arr) -> bool:
        """
        Check whether an array-like is a 1-D pandas sparse array.
    
        Check that the one-dimensional array-like is a pandas sparse array.
        Returns True if it is a pandas sparse array, not another type of
        sparse array.
    
        Parameters
        ----------
        arr : array-like
            Array-like to check.
    
        Returns
        -------
        bool
            Whether or not the array-like is a pandas sparse array.
    
        Examples
        --------
        Returns `True` if the parameter is a 1-D pandas sparse array.
    
        >>> from pandas.api.types import is_sparse
        >>> is_sparse(pd.arrays.SparseArray([0, 0, 1, 0]))
        True
        >>> is_sparse(pd.Series(pd.arrays.SparseArray([0, 0, 1, 0])))
        True
    
        Returns `False` if the parameter is not sparse.
    
        >>> is_sparse(np.array([0, 0, 1, 0]))
        False
        >>> is_sparse(pd.Series([0, 1, 0, 0]))
        False
    
        Returns `False` if the parameter is not a pandas sparse array.
    
        >>> from scipy.sparse import bsr_matrix
        >>> is_sparse(bsr_matrix([0, 1, 0, 0]))
        False
    
        Returns `False` if the parameter has more than one dimension.
        """
>       warnings.warn(
            "is_sparse is deprecated and will be removed in a future "
            "version. Check `isinstance(dtype, pd.SparseDtype)` instead.",
            FutureWarning,
            stacklevel=find_stack_level(),
        )
E       FutureWarning: is_sparse is deprecated and will be removed in a future version. Check `isinstance(dtype, pd.SparseDtype)` instead.

opt/conda/envs/arrow/lib/python3.9/site-packages/pandas/core/dtypes/common.py:210: FutureWarning
_______________ TestConvertMetadata.test_multiindex_doesnt_warn ________________

self = <pyarrow.tests.test_pandas.TestConvertMetadata object at 0x7f09687cea50>

    def test_multiindex_doesnt_warn(self):
        # ARROW-3953: pandas 0.24 rename of MultiIndex labels to codes
        columns = pd.MultiIndex.from_arrays([['one', 'two'], ['X', 'Y']])
        df = pd.DataFrame([(1, 'a'), (2, 'b'), (3, 'c')], columns=columns)
    
        with warnings.catch_warnings():
            warnings.simplefilter(action="error")
>           _check_pandas_roundtrip(df, preserve_index=True)

opt/conda/envs/arrow/lib/python3.9/site-packages/pyarrow/tests/test_pandas.py:297: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
opt/conda/envs/arrow/lib/python3.9/site-packages/pyarrow/tests/test_pandas.py:86: in _check_pandas_roundtrip
    table = klass.from_pandas(df, schema=schema,
pyarrow/table.pxi:3681: in pyarrow.lib.Table.from_pandas
    ???
opt/conda/envs/arrow/lib/python3.9/site-packages/pyarrow/pandas_compat.py:570: in dataframe_to_arrays
    convert_fields) = _get_columns_to_convert(df, schema, preserve_index,
opt/conda/envs/arrow/lib/python3.9/site-packages/pyarrow/pandas_compat.py:373: in _get_columns_to_convert
    if _pandas_api.is_sparse(col):
pyarrow/pandas-shim.pxi:191: in pyarrow.lib._PandasAPIShim.is_sparse
    ???
pyarrow/pandas-shim.pxi:193: in pyarrow.lib._PandasAPIShim.is_sparse
    ???
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

arr = 0    1
1    2
2    3
Name: (one, X), dtype: int64

    def is_sparse(arr) -> bool:
        """
        Check whether an array-like is a 1-D pandas sparse array.
    
        Check that the one-dimensional array-like is a pandas sparse array.
        Returns True if it is a pandas sparse array, not another type of
        sparse array.
    
        Parameters
        ----------
        arr : array-like
            Array-like to check.
    
        Returns
        -------
        bool
            Whether or not the array-like is a pandas sparse array.
    
        Examples
        --------
        Returns `True` if the parameter is a 1-D pandas sparse array.
    
        >>> from pandas.api.types import is_sparse
        >>> is_sparse(pd.arrays.SparseArray([0, 0, 1, 0]))
        True
        >>> is_sparse(pd.Series(pd.arrays.SparseArray([0, 0, 1, 0])))
        True
    
        Returns `False` if the parameter is not sparse.
    
        >>> is_sparse(np.array([0, 0, 1, 0]))
        False
        >>> is_sparse(pd.Series([0, 1, 0, 0]))
        False
    
        Returns `False` if the parameter is not a pandas sparse array.
    
        >>> from scipy.sparse import bsr_matrix
        >>> is_sparse(bsr_matrix([0, 1, 0, 0]))
        False
    
        Returns `False` if the parameter has more than one dimension.
        """
>       warnings.warn(
            "is_sparse is deprecated and will be removed in a future "
            "version. Check `isinstance(dtype, pd.SparseDtype)` instead.",
            FutureWarning,
            stacklevel=find_stack_level(),
        )
E       FutureWarning: is_sparse is deprecated and will be removed in a future version. Check `isinstance(dtype, pd.SparseDtype)` instead.
@AlenkaF AlenkaF added this to the 13.0.0 milestone May 9, 2023
AlenkaF pushed a commit that referenced this issue May 9, 2023
### Rationale for this change

pandas is deprecating `pandas.types.is_sparse` in 2.1

### What changes are included in this PR?

Changing usages of `is_sparse` to the recommended migration `isinstance(dtype, pd.SparseDtype)`

### Are these changes tested?

### Are there any user-facing changes?

No

closes #35329
* Closes: #35329

Authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>
Signed-off-by: Alenka Frim <frim.alenka@gmail.com>
liujiacheng777 pushed a commit to LoongArch-Python/arrow that referenced this issue May 11, 2023
…pache#35366)

### Rationale for this change

pandas is deprecating `pandas.types.is_sparse` in 2.1

### What changes are included in this PR?

Changing usages of `is_sparse` to the recommended migration `isinstance(dtype, pd.SparseDtype)`

### Are these changes tested?

### Are there any user-facing changes?

No

closes apache#35329
* Closes: apache#35329

Authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>
Signed-off-by: Alenka Frim <frim.alenka@gmail.com>
ArgusLi pushed a commit to Bit-Quill/arrow that referenced this issue May 15, 2023
…pache#35366)

### Rationale for this change

pandas is deprecating `pandas.types.is_sparse` in 2.1

### What changes are included in this PR?

Changing usages of `is_sparse` to the recommended migration `isinstance(dtype, pd.SparseDtype)`

### Are these changes tested?

### Are there any user-facing changes?

No

closes apache#35329
* Closes: apache#35329

Authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>
Signed-off-by: Alenka Frim <frim.alenka@gmail.com>
rtpsw pushed a commit to rtpsw/arrow that referenced this issue May 16, 2023
…pache#35366)

### Rationale for this change

pandas is deprecating `pandas.types.is_sparse` in 2.1

### What changes are included in this PR?

Changing usages of `is_sparse` to the recommended migration `isinstance(dtype, pd.SparseDtype)`

### Are these changes tested?

### Are there any user-facing changes?

No

closes apache#35329
* Closes: apache#35329

Authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>
Signed-off-by: Alenka Frim <frim.alenka@gmail.com>
@raulcd raulcd modified the milestones: 13.0.0, 12.0.1 May 31, 2023
raulcd pushed a commit that referenced this issue May 31, 2023
### Rationale for this change

pandas is deprecating `pandas.types.is_sparse` in 2.1

### What changes are included in this PR?

Changing usages of `is_sparse` to the recommended migration `isinstance(dtype, pd.SparseDtype)`

### Are these changes tested?

### Are there any user-facing changes?

No

closes #35329
* Closes: #35329

Authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>
Signed-off-by: Alenka Frim <frim.alenka@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants