Skip to content

BUG: Unexpected behaviour with pd.filter in pandas 2.1 #55075

@OOAmusat

Description

@OOAmusat

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

input_array_pd = pd.DataFrame(
    {
        "x1": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
        "x2": [10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
        "y1": [11, 14, 19, 26, 35, 46, 59, 74, 91, 110],
        "y2": [21, 24, 29, 36, 45, 56, 69, 84, 101, 120],
    }
)

x_lab = ["y1", "x1"]
input_array_pd.filter(x_lab)

Issue Description

Calling pd.filter on a dataframe with a list of columns does not appear to preserve the order specified. It returns the columns in the original order of the dataframe:

>>> input_array_pd.filter(x_lab)

   x1   y1
0   0   11
1   1   14
2   2   19
3   3   26
4   4   35
5   5   46
6   6   59
7   7   74
8   8   91
9   9  110

Expected Behavior

Expected behaviour is for pd.filter to return the columns in exactly the order specified in the list, i.e.,

>>> input_array_pd.filter(x_lab)
    y1  x1
0   11   0
1   14   1
2   19   2
3   26   3
4   35   4
5   46   5
6   59   6
7   74   7
8   91   8
9  110   9

Installed Versions

This is from a version from a version working as expected:

INSTALLED VERSIONS

commit : 0f43794
python : 3.8.13.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 2.0.3
numpy : 1.22.3
pytz : 2022.1
dateutil : 2.8.2
setuptools : 61.2.0
pip : 21.2.2
Cython : None
pytest : 7.1.2
hypothesis : None
sphinx : 5.3.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.8.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 7.33.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.0
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 2.0.1
zstandard : None
tzdata : 2023.3
qtpy : 2.1.0
pyqt5 : None

This is from the newer version producing weird behaviour:

INSTALLED VERSIONS

commit : ba1cccd
python : 3.10.12.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 2.1.0
numpy : 1.24.3
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 68.0.0
pip : 23.2.1
Cython : None
pytest : 7.4.2
hypothesis : None
sphinx : 5.1.1
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.15.0
pandas_datareader : None
bs4 : 4.12.2
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.7.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.11.2
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 2.0.1
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions