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

Inplace boolean setting on mixed-types with a non np.nan value #20326

Closed
nino-rasic opened this issue Mar 13, 2018 · 1 comment
Closed

Inplace boolean setting on mixed-types with a non np.nan value #20326

nino-rasic opened this issue Mar 13, 2018 · 1 comment
Labels
Duplicate Report Duplicate issue or pull request Indexing Related to indexing on series/frames, not to indexes themselves Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate

Comments

@nino-rasic
Copy link

Code Sample,

In [4]: df = pd.DataFrame({'series': pd.Series(range(3))})

In [5]: df["name"] = "abc"

In [6]: df_copy = df.copy()

In [7]: df.series.loc[2] = None

In [8]: df
Out[8]: 
   series name
0     0.0  abc
1     1.0  abc
2     NaN  abc

In [9]: df_copy
Out[9]: 
   series name
0       0  abc
1       1  abc
2       2  abc

In [10]: df[df.isnull()] = df_copy

#or
#df.where(-df.isnull(), df_copy, inplace=True)
#basically it's doing that at the end

Problem description

Pandas throws a TypeError:

Traceback (most recent call last):
  File ".../test_replace.py", line 8, in <module>
    df[df.isnull()] = df_copy
  File ".../anaconda/lib/python2.7/site-packages/pandas/core/frame.py", line 2516, in __setitem__
    self._setitem_frame(key, value)
  File ".../anaconda/lib/python2.7/site-packages/pandas/core/frame.py", line 2552, in _setitem_frame
    self._check_inplace_setting(value)
  File ".../anaconda/lib/python2.7/site-packages/pandas/core/generic.py", line 3738, in _check_inplace_setting
    raise TypeError('Cannot do inplace boolean setting on '
TypeError: Cannot do inplace boolean setting on mixed-types with a non np.nan value

I understand that in-place setting doesn't like to work with the mixed types, but I can't see a reason why it shouldn't work in this case and maybe check in "_check_inplace_setting()" is a bit too tight ?

Expected Output

same as with df.where(-df.isnull(), df_copy)

In [12]: df.where(-df.isnull(), df_copy)
Out[12]: 
   series name
0     0.0  abc
1     1.0  abc
2     2.0  abc
@nino-rasic nino-rasic changed the title Boolean setting on mixed-types with a non np.nan value Inplace boolean setting on mixed-types with a non np.nan value Mar 13, 2018
@jreback
Copy link
Contributor

jreback commented Mar 14, 2018

duplicate of #15613. the current mechanism is not very robust for multi dtype setting. welcome for you to have a look.

@jreback jreback closed this as completed Mar 14, 2018
@jreback jreback added Indexing Related to indexing on series/frames, not to indexes themselves Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Duplicate Report Duplicate issue or pull request labels Mar 14, 2018
@jreback jreback added this to the No action milestone Mar 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate Report Duplicate issue or pull request Indexing Related to indexing on series/frames, not to indexes themselves Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Projects
None yet
Development

No branches or pull requests

2 participants