The line with fillna()
raises the warning even though it is not performed inplace. Why is that?
import pandas as pd
import numpy as np
tuples = [('foo', 1), ('foo', 2), ('bar', 1), ('bar', 2)]
index = pd.MultiIndex.from_tuples(tuples)
df = pd.DataFrame(np.random.randn(2, 4), columns=index)
df.loc[0, ('foo', 1)] = np.nan
# this works without warning
# df = pd.DataFrame({'foo': [1, np.nan, 3], 'bar': [np.nan, 22, 33]]})
df1 = df[['foo', 'bar']]
# df1 = df[['foo', 'bar']].copy() # this does not help
filled = df1.fillna({'foo': 100, 'bar': 200}, inplace=False)
The problem does not appear if foo
and bar
are ordinary columns, not multiindexed.