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

(WIP) BUG: DatetimeTZBlock.fillna raises TypeError #11153

Merged
merged 1 commit into from
Sep 20, 2015

Conversation

sinhrks
Copy link
Member

@sinhrks sinhrks commented Sep 19, 2015

import pandas as pd
idx = pd.DatetimeIndex(['2011-01-01', pd.NaT, '2011-01-03'], tz='Asia/Tokyo')
s = pd.Series(idx)
s
#0   2011-01-01 00:00:00+09:00
#1                         NaT
#2   2011-01-03 00:00:00+09:00
# dtype: datetime64[ns, Asia/Tokyo]

# NG, the result must be DatetimeTZBlock
s.fillna(pd.Timestamp('2011-01-02', tz='Asia/Tokyo'))
# TypeError: putmask() argument 1 must be numpy.ndarray, not DatetimeIndex

# NG, it should be object dtype, as the same as when Series creation with mixed tz
s.fillna(pd.Timestamp('2011-01-02'))
# TypeError: putmask() argument 1 must be numpy.ndarray, not DatetimeIndex

Also, found existing DatetimeBlock doesn't handle tz properly. Closes #7095.

idx = pd.DatetimeIndex(['2011-01-01', pd.NaT, '2011-01-03'])
s = pd.Series(idx)

# OK, result must be DatetimeBlock
s.fillna(pd.Timestamp('2011-01-02'))
#0   2011-01-01
#1   2011-01-02
#2   2011-01-03
# dtype: datetime64[ns]

# NG, it should be object dtype, as the same as when Series creation with mixed tz
s.fillna(pd.Timestamp('2011-01-02', tz='Asia/Tokyo'))
#0   2011-01-01 00:00:00
#1   2011-01-01 15:00:00
#2   2011-01-03 00:00:00
# dtype: datetime64[ns]

# NG, unable to fill different dtypes
s.fillna('AAA')
# ValueError: Error parsing datetime string "AAA" at position 0

@sinhrks sinhrks added Bug Datetime Datetime data dtype Timezones Timezone data dtype Blocker Blocking issue or pull request for an upcoming release labels Sep 19, 2015
@sinhrks sinhrks added this to the 0.17.0 milestone Sep 19, 2015
@jreback
Copy link
Contributor

jreback commented Sep 19, 2015

this is #7095, yes?

@@ -740,6 +740,18 @@ def _add_offset(self, offset):
PerformanceWarning)
return self.astype('O') + offset

def putmask(self, mask, value):
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hah, already exists in core/index.py

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, shoud I include the logic to putmask? I think it is better, but I concerned it may break something.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah my explanation is incorrect. This is remained because of my early attempt, it can be removed.

@sinhrks
Copy link
Member Author

sinhrks commented Sep 19, 2015

Correct, this should fix #7095. I'll add test case for this.

Also, I found current impl can't handle inplace option properly. inplace should be effective if target contains no NaN values in case of DatetimeTZBlock.

@sinhrks sinhrks changed the title BUG: DatetimeTZBlock.fillna raises TypeError (WIP) BUG: DatetimeTZBlock.fillna raises TypeError Sep 19, 2015
if mask.any():
try:
return self._fillna_mask(mask, value, inplace=inplace)
except TypeError:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need to test this because you know inplace, e.g. just use an if?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added mask.any() condition to handle the case when the block contains no Nan. In this case:

  • We can return the block as it is.
  • Don't want to coerce data to object even if the fill value is object.
  • DatetimeTZBlock can perform inplace op in this case. Otherwise can't because it's internal value is DatetimeIndex.

Let me do a follow-up if I do something wrong.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hah...i already merged. the code looked reasonable.

have a look and see if anything needs cleaning (or not ok too).

thanks!

jreback added a commit that referenced this pull request Sep 20, 2015
(WIP) BUG: DatetimeTZBlock.fillna raises TypeError
@jreback jreback merged commit 8c9192c into pandas-dev:master Sep 20, 2015
@jreback
Copy link
Contributor

jreback commented Sep 20, 2015

thanks @sinhrks nice catch

@sinhrks sinhrks deleted the fillna_tzbug branch September 26, 2015 00:27
Dr-Irv added a commit to Dr-Irv/pandas that referenced this pull request Oct 24, 2015
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
Labels
Blocker Blocking issue or pull request for an upcoming release Bug Datetime Datetime data dtype Timezones Timezone data dtype
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants