2

I am cleaning some data (its been a while) and when I drop the rows that have a nan within either of two specific columns I am left with a "noneType" object that I cannot run any further functions on. why is this happening and how do i fix this.

I expected this to drop rows that had nans in those columns but did not expect it to change to 'noneType'

df = df.dropna(subset=['Step Age', 'Process Age'], inplace = True)

ALollz
  • 57,915
  • 7
  • 66
  • 89
db4
  • 41
  • 1
  • 5
  • 2
    Where is the Dataframe? Where is the code that actually calls `dropna`. Please create a [mcve] – user3483203 May 01 '19 at 19:59
  • sorry! pasted wrong line of code just noticed that – db4 May 01 '19 at 20:11
  • df.dropna(subset=['Step Age', 'Process Age'], inplace = True) – db4 May 01 '19 at 20:11
  • 1
    Are you accidentally assigning the result back: `df = df.dropna(inplace=True)`? Inplace operations will return None. – ALollz May 01 '19 at 20:53
  • I am. is the "df =" part unneccesary? – db4 May 01 '19 at 21:01
  • 1
    It's not just unnecessary, it's the problem. You either do `df = df.dropna(inplace=False)` or `df.dropna(inplace=True)` to modify `df`. (`inplace=False` is the default, so it suffices to just do `df = df.dropna()`, though I left it there to be explicit) – ALollz May 01 '19 at 21:03
  • This question is relevant: https://stackoverflow.com/questions/43893457/python-pandas-understanding-inplace-true – ALollz May 01 '19 at 21:06

1 Answers1

1

A dataframe cannot become a NoneType object. Instead, various operations may empty the dataframe. In the question, the dataframe has been deallocated.

import pandas as pd
import csv
from pandas.compat import StringIO

print(pd.__version__)

csvdata = StringIO("""Step Age,Process Age,Extra Col
,1,
1,,
,,""")

df = pd.read_csv(csvdata, sep=",")
df.dropna(subset=['Step Age', 'Process Age'], inplace = True)
print(df)
print(df.empty)

produces

0.24.2
Empty DataFrame
Columns: [Step Age, Process Age, Extra Col]
Index: []
True

Rich Andrews
  • 1,590
  • 8
  • 12