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

Negative NaN (-nan) in CSV input treated as object #5952

Closed
Bklyn opened this issue Jan 15, 2014 · 3 comments · Fixed by #6038
Closed

Negative NaN (-nan) in CSV input treated as object #5952

Bklyn opened this issue Jan 15, 2014 · 3 comments · Fixed by #6038
Labels
API Design IO CSV read_csv, to_csv Numeric Operations Arithmetic, Comparison, and Logical operations
Milestone

Comments

@Bklyn
Copy link
Contributor

Bklyn commented Jan 15, 2014

The value "-nan" (yes, this is a valid type of NaN!) in a CSV input file causes that column to be treated as 'object' instead of float64.

pd.read_csv(StringIO.StringIO('a,b\n1,2.0\n2,nan\n3,-nan')).b
Out[15]: 
0     2.0
1     NaN
2    -nan
Name: b, dtype: object

pd.read_csv(StringIO.StringIO('a,b\n1,2.0\n2,nan\n')).b
Out[16]: 
0     2
1   NaN
Name: b, dtype: float64

When the file is sufficiently large, the following error is generated:

In [3]: pd.read_csv('big.bad.csv')
/usr/local/lib/python2.7/dist-packages/pandas/io/parsers.py:1033: DtypeWarning: Columns (58,64) have mixed types. Specify dtype option on import or set low_memory=False.
  data = self._reader.read(nrows)

If the string "-nan" is replaced with "nan" all is well. I don't really need to distinguish negative NaN from NaN but would like to be able to read my data files w/o having to pre-process them to scrub all the '-nan's.

@jreback
Copy link
Contributor

jreback commented Jan 15, 2014

try specifying it as a na_value see here: http://pandas.pydata.org/pandas-docs/dev/io.html#na-values
lmk if this works; what version of pandas?

@jreback
Copy link
Contributor

jreback commented Jan 15, 2014

pd.read_csv(StringIO.StringIO('a,b\n1,2.0\n2,nan\n3,-nan'), na-values=['-nan']).b

works

@jreback jreback closed this as completed Jan 15, 2014
@jreback
Copy link
Contributor

jreback commented Jan 15, 2014

I don't have a problem with making -nan a default nan value; if you would like to do a PR would be great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design IO CSV read_csv, to_csv Numeric Operations Arithmetic, Comparison, and Logical operations
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants