8000 Fix FillMissing processor by removing inplace=True by nikhilmalkari8 · Pull Request #4055 · fastai/fastai · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix FillMissing processor by removing inplace=True #4055

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fastai/tabular/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def encodes(self, to):
for n in missing.any()[missing.any()].keys():
assert n in self.na_dict, f"nan values in `{n}` but not in setup training set"
for n in self.na_dict.keys():
to[n].fillna(self.na_dict[n], inplace=True)
to[n] = to[n].fillna(self.na_dict[n])
if self.add_col:
to.loc[:,n+'_na'] = missing[n]
if n+'_na' not in to.cat_names: to.cat_names.append(n+'_na')
Expand Down
27 changes: 27 additions & 0 deletions tests/test_tabular_core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pandas as pd
from fastai.tabular.core import FillMissing, TabularPandas

def test_fillna():
# Mock data
df = pd.DataFrame({"a": [1, None, 3], "b": [4, 5, None]})
na_dict = {"a": 0, "b": -1}

# Initialize TabularPandas with appropriate columns
tab_pandas = TabularPandas(
df,
procs=[], # No preprocessing steps required
cont_names=["a", "b"],
cat_names=[],
y_names=[]
)

# Initialize FillMissing
fill_missing = FillMissing(add_col=False)
fill_missing.na_dict = na_dict # Manually set the na_dict for testing

# Apply the transformation
fill_missing.encodes(tab_pandas)

# Check results
assert (tab_pandas["a"] == [1, 0, 3]).all()
assert (tab_pandas["b"] == [4, 5, -1]).all()
0