Skip to content

BUG: com.maybe_make_list does not resolve correctly for np.array #44056

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

Open
3 tasks done
erfannariman opened this issue Oct 16, 2021 · 2 comments
Open
3 tasks done

BUG: com.maybe_make_list does not resolve correctly for np.array #44056

erfannariman opened this issue Oct 16, 2021 · 2 comments
Labels

Comments

@erfannariman
Copy link
Member

erfannariman commented Oct 16, 2021

  • 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 master branch of pandas.

Reproducible Example

from pandas.core.common import maybe_make_list
import numpy as np

arr = np.array(["A", "C"])
result = maybe_make_list(arr)
print(result)

[array(['A', 'C'], dtype='<U1')]

Issue Description

This discussion started in this thread. Should com.maybe_make_list take np.array into account and maybe some other list like objects as well. For example Index objects.

Expected Behavior

from pandas.core.common import maybe_make_list
import numpy as np

arr = np.array(["A", "C"])
result = maybe_make_list(arr)
print(result)

array(['A', 'C'], dtype='<U1')

Installed Versions

INSTALLED VERSIONS

commit : 445bb9f
python : 3.8.12.final.0
python-bits : 64
OS : Darwin
OS-release : 20.6.0
Version : Darwin Kernel Version 20.6.0: Mon Aug 30 06:12:21 PDT 2021; root:xnu-7195.141.6~3/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 1.4.0.dev0+904.g445bb9f57c.dirty
numpy : 1.21.2
pytz : 2021.3
dateutil : 2.8.2
pip : 21.2.4
setuptools : 58.0.4
Cython : 0.29.24
pytest : 6.2.5
hypothesis : 6.23.2
sphinx : 4.2.0
blosc : 1.10.6
feather : None
xlsxwriter : 3.0.1
lxml.etree : 4.6.3
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.0.2
IPython : 7.28.0
pandas_datareader: None
bs4 : 4.10.0
bottleneck : 1.3.2
fsspec : 2021.10.0
fastparquet : 0.7.1
gcsfs : 2021.10.0
matplotlib : 3.4.3
numexpr : 2.7.3
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : 5.0.0
pyxlsb : None
s3fs : 2021.10.0
scipy : 1.7.1
sqlalchemy : 1.4.25
tables : 3.6.1
tabulate : 0.8.9
xarray : 0.18.2
xlrd : 2.0.1
xlwt : 1.3.0
numba : None

@rhshadrach
Copy link
Member

rhshadrach commented Oct 18, 2021

I propose the following methods instead of the current maybe_make_list:

def maybe_make_list_like(obj):
    if obj is None or is_list_like(obj):
        return obj
    else:
        return [obj]

def maybe_make_list(obj) -> list:
    obj = maybe_make_list_like(obj)
    if obj is not None and not isinstance(obj, list):
        obj = list(obj)
    return obj

Currently, it is used in:

@erfannariman
Copy link
Member Author

erfannariman commented Oct 18, 2021

I propose the following methods instead of the current maybe_make_list:

def maybe_make_list_like(obj):
    if obj is None or is_list_like(obj):
        return obj
    else:
        return [obj]

def maybe_make_list(obj) -> list:
    obj = maybe_make_list_like(obj)
    if obj is not None and not isinstance(obj, list):
        obj = list(obj)
    return obj

Currently, it is used in:

Some quick tests, but this looks like behaving as expected:

In [8]: maybe_make_list(np.array(["A", "B"]))
Out[8]: ['A', 'B']

In [9]: maybe_make_list("A")
Out[9]: ['A']

In [10]: 

In [10]: maybe_make_list(("A",))
Out[10]: ['A']

In [11]: maybe_make_list(pd.Index(["A", "B"]))
Out[11]: ['A', 'B']

Looks good.

@mroeschke mroeschke removed the Needs Triage Issue that has not been reviewed by a pandas team member label Oct 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants