Open jrbourbeau opened 1 year ago
This function returns a boolean or array-like of bool. I'll keep the "bug" label just in case, but I don't think it is one.
https://pandas.pydata.org/docs/reference/api/pandas.isnull.html
Thanks @DeaMariaLeon. The thing that seems off to me is pd.isnull
is treating lists as array-like (returning a array-like of bools) and tuples as scalar (returning a bool)
In [1]: import pandas as pd
In [2]: pd.isnull([1, pd.NA, 3])
Out[2]: array([False, True, False])
In [3]: pd.isnull((1, pd.NA, 3))
Out[3]: False
My expectation is that both lists and tuples should be treated as array-like. Though feel free to let me know if that expectation is incorrect
Oh, I see! Thank you for opening an issue. :)
This is an edge case I think.
You can end up with tuples from a MultiIndex for example. In this scenario we want to treat the tuple as a single element, e.g.
df.drop(columns=(1, 2))
treats the tuple as a single element. I think this is similar here although it does not really look intuitive to me either.
Interestingly in a list, tuples are treated as array-like:
obj = [(1.0, 2.0), (1.0, np.nan), (np.nan, 2.0), (np.nan, np.nan)]
print(pd.isnull(obj))
# [[False False]
# [False True]
# [ True False]
# [ True True]]
treating lists as array-like (returning a array-like of bools) and tuples as scalar (returning a bool)
As far as I remember, in the past we made this distinction (in certain places) because tuples can be labels, as Patrick mentioned.
But it's indeed a tricky situation, with easy confusion and corner cases (a quick search for "tuple list label" gives quite some related issues). For example https://github.com/pandas-dev/pandas/issues/43978 for the drop example.
Another example in indexing where the two are distinguished and have different behaviour:
>>> s = pd.Series(range(6), index=pd.MultiIndex.from_product([[1, 2, 3], [1, 2]]))
>>> s.loc[(1, 2)] # tuple is a single label
1
>>> s.loc[[1, 2]] # list is an indexer (in this case for the first level of the MultiIndex)
1 1 0
2 1
2 1 2
2 3
dtype: int64
Pandas version checks
[X] I have checked that this issue has not already been reported.
[X] 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
Issue Description
It looks like
pd.isnull
is treatinglist
as array-like andtuple
as a scalarExpected Behavior
I'd expect
list
s andtuple
s to be treated similarly bypd.isnull
. Similar to other parts of the API likepd.Series
Installed Versions