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

DataFrame with TimeSeries column raises TypeError in 0.17.0 when replacing ints or floats #11326

Closed
joshowen opened this Issue Oct 14, 2015 · 2 comments

Comments

Projects
None yet
2 participants
@joshowen
Contributor

joshowen commented Oct 14, 2015

Example:

>>> df = extended_df
Traceback (most recent call last):
  File "/Users/josh/anaconda/envs/Openfolio/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 3066, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-14-3f216cf6821e>", line 1, in <module>
    df = extended_df
NameError: name 'extended_df' is not defined
>>> df = expanded
>>> df
Out[16]: 
                             pricing_date  exchange_id  close
pricing_date_ts                                              
1445025600      2015-10-13 20:00:00+00:00          NaN    NaN
1444939200      2015-10-13 20:00:00+00:00          NaN    NaN
1444852800      2015-10-13 20:00:00+00:00            0       NaN
1444766400      2015-10-13 20:00:00+00:00            6  0.545
1444680000      2015-10-12 20:00:00+00:00            6  0.570
1444420800      2015-10-09 20:00:00+00:00            6  0.580
1444334400      2015-10-08 20:00:00+00:00            6  0.560
1444248000      2015-10-07 20:00:00+00:00            6  0.580
1444161600      2015-10-06 20:00:00+00:00            6  0.620
1444075200      2015-10-05 20:00:00+00:00            6  0.480
1443816000      2015-10-13 20:00:00+00:00          NaN    NaN
>>> df.dtypes
Out[17]: 
pricing_date    datetime64[ns, UTC]
exchange_id                 float64
close                       float64
dtype: object
>>> df.replace(0, np.NaN)
Traceback (most recent call last):
  File "/Users/josh/anaconda/envs/Openfolio/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 3066, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-18-cc26b9ec3ff7>", line 1, in <module>
    df.replace(0, np.NaN)
  File "/Users/josh/anaconda/envs/Openfolio/lib/python2.7/site-packages/pandas/core/generic.py", line 2996, in replace
    inplace=inplace, regex=regex)
  File "/Users/josh/anaconda/envs/Openfolio/lib/python2.7/site-packages/pandas/core/internals.py", line 2761, in replace
    return self.apply('replace', **kwargs)
  File "/Users/josh/anaconda/envs/Openfolio/lib/python2.7/site-packages/pandas/core/internals.py", line 2710, in apply
    applied = getattr(b, f)(**kwargs)
  File "/Users/josh/anaconda/envs/Openfolio/lib/python2.7/site-packages/pandas/core/internals.py", line 560, in replace
    mask = com.mask_missing(self.values, to_replace)
  File "/Users/josh/anaconda/envs/Openfolio/lib/python2.7/site-packages/pandas/core/common.py", line 449, in mask_missing
    mask = arr == x
  File "/Users/josh/anaconda/envs/Openfolio/lib/python2.7/site-packages/pandas/tseries/index.py", line 84, in wrapper
    other = _ensure_datetime64(other)
  File "/Users/josh/anaconda/envs/Openfolio/lib/python2.7/site-packages/pandas/tseries/index.py", line 111, in _ensure_datetime64
    raise TypeError('%s type object %s' % (type(other), str(other)))
TypeError: <type 'int'> type object 0
@jreback

This comment has been minimized.

Show comment
Hide comment
@jreback

jreback Oct 14, 2015

Contributor

here's a simpler example

In [79]: df = DataFrame({'A' : date_range('20130101',periods=3,tz='US/Eastern'), 'B' : [0, np.nan, 2]})

In [80]: df
Out[80]: 
                          A   B
0 2013-01-01 00:00:00-05:00   0
1 2013-01-02 00:00:00-05:00 NaN
2 2013-01-03 00:00:00-05:00   2

In [81]: df.dtypes
Out[81]: 
A    datetime64[ns, US/Eastern]
B                       float64
dtype: object

In [82]: df.replace(np.nan,1)
Out[82]: 
                          A  B
0 2013-01-01 00:00:00-05:00  0
1 2013-01-02 00:00:00-05:00  1
2 2013-01-03 00:00:00-05:00  2

In [83]: df.fillna(1)
Out[83]: 
                          A  B
0 2013-01-01 00:00:00-05:00  0
1 2013-01-02 00:00:00-05:00  1
2 2013-01-03 00:00:00-05:00  2

In [84]: df.replace(0,np.nan)
TypeError: <type 'int'> type object 0
Contributor

jreback commented Oct 14, 2015

here's a simpler example

In [79]: df = DataFrame({'A' : date_range('20130101',periods=3,tz='US/Eastern'), 'B' : [0, np.nan, 2]})

In [80]: df
Out[80]: 
                          A   B
0 2013-01-01 00:00:00-05:00   0
1 2013-01-02 00:00:00-05:00 NaN
2 2013-01-03 00:00:00-05:00   2

In [81]: df.dtypes
Out[81]: 
A    datetime64[ns, US/Eastern]
B                       float64
dtype: object

In [82]: df.replace(np.nan,1)
Out[82]: 
                          A  B
0 2013-01-01 00:00:00-05:00  0
1 2013-01-02 00:00:00-05:00  1
2 2013-01-03 00:00:00-05:00  2

In [83]: df.fillna(1)
Out[83]: 
                          A  B
0 2013-01-01 00:00:00-05:00  0
1 2013-01-02 00:00:00-05:00  1
2 2013-01-03 00:00:00-05:00  2

In [84]: df.replace(0,np.nan)
TypeError: <type 'int'> type object 0

@jreback jreback added this to the 0.17.1 milestone Oct 14, 2015

@jreback

This comment has been minimized.

Show comment
Hide comment
@jreback

jreback Oct 14, 2015

Contributor

Of course you can do this

In [85]: df['B'] = df['B'].replace(0,np.nan)

In [86]: df
Out[86]: 
                          A   B
0 2013-01-01 00:00:00-05:00 NaN
1 2013-01-02 00:00:00-05:00 NaN
2 2013-01-03 00:00:00-05:00   2
Contributor

jreback commented Oct 14, 2015

Of course you can do this

In [85]: df['B'] = df['B'].replace(0,np.nan)

In [86]: df
Out[86]: 
                          A   B
0 2013-01-01 00:00:00-05:00 NaN
1 2013-01-02 00:00:00-05:00 NaN
2 2013-01-03 00:00:00-05:00   2

jreback added a commit that referenced this issue Oct 17, 2015

Merge pull request #11329 from jreback/replace
BUG: Bug in DataFrame.replace with a datetime64[ns, tz] and a non-compat to_replace #11326

Dr-Irv added a commit to Dr-Irv/pandas that referenced this issue Oct 24, 2015

Fix for issue #11317
This includes updates to 3 Excel files, plus a test in test_excel.py,
plus the fix in parsers.py

issue when read_html with previous fix

With read_html, the fix didn't work on Python 2.7.  Handle the string
conversion correctly

Add bug fixed to what's new

Revert "Add bug fixed to what's new"

This reverts commit 05b2344.

Revert "issue when read_html with previous fix"

This reverts commit d1bc296.

Add what's new to describe bug.  fix issue with original fix

Added text to describe the bug.
Fixed issue so that it works correctly in Python 2.7

Add round trip test

Added round trip test and fixed error in writing sheets when
merge_cells=false and columns have multi index

DEPR: deprecate pandas.io.ga, pandas-dev#11308

DEPR: deprecate engine keyword from to_csv pandas-dev#11274

remove warnings from the tests for deprecation of engine in to_csv

PERF: Checking monotonic-ness before sorting on an index pandas-dev#11080

BUG: Bug in list-like indexing with a mixed-integer Index, pandas-dev#11320

Add hex color strings test

CLN: GH11271 move _get_handle, UTF encoders to io.common

TST: tests for list skiprows in read_excel

BUG: Fix to_dict() problem when using only datetime pandas-dev#11247

Fix a bug where to_dict() does not return Timestamp when there is only
datetime dtype present.

Undo change for when columns are multiindex

There is still something wrong here in the format of the file when there
are multiindex columns, but that's for another day

Fix formatting in test_excel and remove spurious test

See title

BUG: bug in comparisons vs tuples, pandas-dev#11339

bug#10442 : fix, adding note and test

BUG pandas-dev#10442(test) : Convert datetimelike index to strings with astype(str)

BUG#10422: note added

bug#10442 : tests added

bug#10442 : note udated

BUG pandas-dev#10442(test) : Convert datetimelike index to strings with astype(str)

bug#10442: fix, adding note and test

bug#10442: fix, adding note and test

Adjust test so that merge_cells=False works correctly

Adjust the test so that if merge_cells=false, it does a proper
formatting of the columns in the single row header, and puts the row
header in the first row

Fix test for Python 2.7 and 3.5

The test is failing on Python 2.7 and 3.5, which appears to read in the
values as floats, and I cannot replicate.  So force the tests to pass by
just making the column names equal when merge_cells=False

Fix for openpyxl < 2, and for issue pandas-dev#11408

If using openpyxl < 2, and value is a string that could be a number,
force a string to be written out.  If using openpyxl >= 2.2, then fix
issue pandas-dev#11408 to do with merging cells

Use set_value_explicit instead of set_explicit_value

set_value_explicit is in openpyxl 1.6, changed in openpyxl 1.8, but
there is code in 1.8 to set set_value_explicit to set_explicit_value for
compatibility

Add line in whatsnew for issue 11408

ENH: added capability to handle Path/LocalPath objects, pandas-dev#11033

DOC: typo in whatsnew/0.17.1.txt

PERF: Release GIL on some datetime ops

BUG: Bug in DataFrame.replace with a datetime64[ns, tz] and a non-compat to_replace pandas-dev#11326

CLN: clean up internal impl of fillna/replace, xref pandas-dev#11153

PERF: fast inf checking in to_excel

PERF: Series.dropna with non-nan dtypes

fixed pathlib tests on windows

DEPR: remove some SparsePanel deprecation warnings in testing

DEPR: avoid numpy comparison to None warnings

API: indexing with a null key will raise a TypeError rather than a ValueError, pandas-dev#11356

WARN: elementwise comparisons with index names, xref pandas-dev#11162

DEPR warning in io/data.py w.r.t. order->sort_values

WARN: more elementwise comparisons to object

WARN: more uncomparables of numeric array vs object

BUG: quick fix for pandas-dev#10989

TST: add test case from Issue pandas-dev#10989

API: add _to_safe_for_reshape to allow safe insert/append with embedded CategoricalIndexes

Signed-off-by: Jeff Reback <jeff@reback.net>

BLD: conda

Revert "BLD: conda"

This reverts commit 0c8a8e1.

TST: remove invalid symbol warnings

TST: move some tests to slow

TST: fix some warnings filters

TST: import pandas_datareader, use for tests

TST: remove some deprecation warnings from imports

DEPR: fix VisibleDeprecationWarnings in sparse

TST: remove some warnings in test_nanops

ENH: Improve the error message in to_gbq when the DataFrame schema does not match pandas-dev#11359

add libgfortran to 1.8.1 build

binstar -> anaconda

remove link to issue 11328 in whatsnew

Fixes to document issue in code, small efficiency fix

Try to resolve rebase conflict in whats new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment