-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
BUG: df.fillna(dict, inplace=True) triggers FutureWarning in 1.5.0rc0 when DataFrame is empty #48480
Comments
Hi, thanks for your report. This is fixed on main and also on 1.5.x that will be release soonish, see #48254 |
take |
Did this fix make it into 1.5.0? It seems to me that recently released 1.5.0 still shows this FutureWarning. |
I can not see the warning on 1.5.0. Can you provide your environment information? |
It is a mystery... when using pandas-1.5.0 on MacOS and Linux, plain, I do not see the warning either. In context of my greater program, the FutureWarning keeps appearing - I need to debug further. Maybe some issue with venv, will comment here again with new findings. |
For completeness, below are the installed versions, printed from within the same run that also emits the FutureWarnings. However, I noticed that because of a mistake, I have been importing pandas twice: import pandas; ...; import pandas as pd. Maybe that causes issues? I have fixed this, running now regressions, and will follow up. INSTALLED VERSIONScommit : 87cfe4e pandas : 1.5.0 |
This is odd; I am running the corrected code, and I am still getting:
The code emitting the warning is this statement:
Again, below is the corresponding version list from pandas. I cannot reproduce the FutureWarning with pandas-1.5.0 outside of the program. The warning seems to come out of INSTALLED VERSIONScommit : 87cfe4e pandas : 1.5.0 |
As long as there isn't anything we can debug, there is not much we can do |
Found the corner case - a dataframe with no rows:
Seems that this situation needs to be handled specifically. Sorry for the noise in the previous comments. |
I'm having a similar problem where I apply a function to a grouped dataframe that uses Is there a way to break into pdb when the warning is elicited? I'm not sure how to go about debugging this. |
I am having the same warning issued from the following statement: df.fillna(value={
'ITEM': '<NA>',
'TTYPE': '-1',
'QTY': 0,
'P': -1,
'COUNTRY': '<NA>',
'CITY': '<NA>'
}, inplace=True) When I remove the inplace and instead use: df = df.fillna(...) The warning disappears. I am running 1.5.1 with python 3.10.6 on windows. |
Can you provide something reproducible? |
Even though I am getting the warning in my program every time, I cannot write something that reproduces the warning. My code is doing something like the following (where df has many more columns and rows): import numpy as np
import pandas as pd
df = pd.DataFrame(
{
'ITEM': ['ITM1', 'ITM2', pd.NA, np.nan],
'REF': ['R1', 'R2', 'R3', pd.NA],
'TYPE': -1,
'QTY': 0,
'P': [0, 1, 'Y', 'X'],
}
)
print(f'before:\n{df=}')
df['P'] = pd.to_numeric(df['P'], errors='coerce')
print(f'after to_numeric:\n{df=}')
df.fillna(value={
'ITEM': '<NA>',
'IREF': '<NA>',
'P': -1}, inplace=True)
print(f'tidy:\n{df=}') I will keep trying and report back. |
I'm using pandas 1.5.2 on MacOS 13.0.1, with Python@3.11.0 from Homebrew, and I can consistently reproduce the bug with the code below: import pandas as pd
df = pd.DataFrame(data={'col1': ['a', pd.NA, 'c']}, index=range(3))
df = df.convert_dtypes()
df.fillna({'col1': '<NA>'}, inplace=True) Three conditions together for the bug:
Maybe some problem associated with string dtype? And I also investigate other circumstances.
Maybe some help. |
I confirm that also in my case (see above) if I use df.convert_dtypes() I can reproduce the warning. However, in my original code (which I tried to summarize), I am not using convert_dtypes() explicitly, and I am getting the warning. import numpy as np
import pandas as pd
df = pd.DataFrame(
{
'ITEM': ['ITM1', 'ITM2', pd.NA, np.nan],
'REF': ['R1', 'R2', 'R3', pd.NA],
'TYPE': -1,
'QTY': 0,
'P': [0, 1, 'Y', 'X'],
}
)
print(f'before:\n{df=}')
# This will reproduce the warning
df = df.convert_dtypes()
df['P'] = pd.to_numeric(df['P'], errors='coerce')
print(f'after to_numeric:\n{df=}')
df.fillna(value={
'ITEM': '<NA>',
'IREF': '<NA>',
'P': -1}, inplace=True)
print(f'tidy:\n{df=}') |
Hey @marekro Thanks for having reported this - there's now a release candidate for pandas 2.0.0, which you can install with
or
If you try it out and report bugs, then we can fix them before the final 2.0.0 release |
@MarcoGorelli the above warning is not happening with 2.0.0rc0. However, I am seeing other issues that I will report separately. |
I confirm that I am not seeing the above warning after many tests. And, once I took care of some of the warnings related to some depreciated code, all worked perfectly with 2.0.0rc0. Thank you. |
thanks for checking! |
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
Issue Description
With the most recent development version 1.5.0rc0, I see that the code example raises the following warning:
FutureWarning: In a future version,
df.iloc[:, i] = newvalswill attempt to set the values inplace instead of always setting a new array. To retain the old behavior, use either
df[df.columns[i]] = newvalsor, if columns are non-unique,
df.isetitem(i, newvals)``The df. fillna() API should not use code that is ambiguous or deprecated.
Expected Behavior
df.fillna() should do its job, silently.
Installed Versions
The text was updated successfully, but these errors were encountered: