Skip to content

Commit 4082c1a

Browse files
committed
Merge pull request #7593 from jreback/timedelta_nat
BUG: Bug in timedelta inference when assigning an incomplete Series (GH7592)
2 parents 732a2e7 + bb2fd41 commit 4082c1a

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

doc/source/v0.14.1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ Bug Fixes
172172
- Regression in datetimelike slice indexing with a duplicated index and non-exact end-points (:issue:`7523`)
173173
- Bug in setitem with list-of-lists and single vs mixed types (:issue:`7551`:)
174174
- Bug in timeops with non-aligned Series (:issue:`7500`)
175-
175+
- Bug in timedelta inference when assigning an incomplete Series (:issue:`7592`)
176176

177177
- Bug in ``value_counts`` where ``NaT`` did not qualify as missing (``NaN``) (:issue:`7423`)
178178

pandas/core/common.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2603,7 +2603,7 @@ def check_main():
26032603
def in_qtconsole():
26042604
"""
26052605
check if we're inside an IPython qtconsole
2606-
2606+
26072607
DEPRECATED: This is no longer needed, or working, in IPython 3 and above.
26082608
"""
26092609
try:
@@ -2622,7 +2622,7 @@ def in_qtconsole():
26222622
def in_ipnb():
26232623
"""
26242624
check if we're inside an IPython Notebook
2625-
2625+
26262626
DEPRECATED: This is no longer used in pandas, and won't work in IPython 3
26272627
and above.
26282628
"""

pandas/core/internals.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -1037,9 +1037,11 @@ class FloatBlock(FloatOrComplexBlock):
10371037
def _can_hold_element(self, element):
10381038
if is_list_like(element):
10391039
element = np.array(element)
1040-
return issubclass(element.dtype.type, (np.floating, np.integer))
1041-
return (isinstance(element, (float, int, np.float_, np.int_)) and
1042-
not isinstance(bool, np.bool_))
1040+
tipo = element.dtype.type
1041+
return issubclass(tipo, (np.floating, np.integer)) and not issubclass(
1042+
tipo, (np.datetime64, np.timedelta64))
1043+
return isinstance(element, (float, int, np.float_, np.int_)) and not isinstance(
1044+
element, (bool, np.bool_, datetime, timedelta, np.datetime64, np.timedelta64))
10431045

10441046
def _try_cast(self, element):
10451047
try:
@@ -1099,7 +1101,8 @@ class IntBlock(NumericBlock):
10991101
def _can_hold_element(self, element):
11001102
if is_list_like(element):
11011103
element = np.array(element)
1102-
return issubclass(element.dtype.type, np.integer)
1104+
tipo = element.dtype.type
1105+
return issubclass(tipo, np.integer) and not issubclass(tipo, (np.datetime64, np.timedelta64))
11031106
return com.is_integer(element)
11041107

11051108
def _try_cast(self, element):

pandas/tests/test_frame.py

+26-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
import pandas.core.format as fmt
3333
import pandas.core.datetools as datetools
3434
from pandas import (DataFrame, Index, Series, notnull, isnull,
35-
MultiIndex, DatetimeIndex, Timestamp, date_range, read_csv)
35+
MultiIndex, DatetimeIndex, Timestamp, date_range, read_csv,
36+
_np_version_under1p7)
3637
import pandas as pd
3738
from pandas.parser import CParserError
3839
from pandas.util.misc import is_little_endian
@@ -2180,11 +2181,11 @@ def test_set_index_cast_datetimeindex(self):
21802181
# reset_index with single level
21812182
for tz in ['UTC', 'Asia/Tokyo', 'US/Eastern']:
21822183
idx = pd.date_range('1/1/2011', periods=5, freq='D', tz=tz, name='idx')
2183-
df = pd.DataFrame({'a': range(5), 'b': ['A', 'B', 'C', 'D', 'E']}, index=idx)
2184+
df = pd.DataFrame({'a': range(5), 'b': ['A', 'B', 'C', 'D', 'E']}, index=idx)
21842185

21852186
expected = pd.DataFrame({'idx': [datetime(2011, 1, 1), datetime(2011, 1, 2),
21862187
datetime(2011, 1, 3), datetime(2011, 1, 4),
2187-
datetime(2011, 1, 5)],
2188+
datetime(2011, 1, 5)],
21882189
'a': range(5), 'b': ['A', 'B', 'C', 'D', 'E']},
21892190
columns=['idx', 'a', 'b'])
21902191
expected['idx'] = expected['idx'].apply(lambda d: pd.Timestamp(d, tz=tz))
@@ -3757,6 +3758,28 @@ def test_operators_timedelta64(self):
37573758
self.assertTrue(df['off1'].dtype == 'timedelta64[ns]')
37583759
self.assertTrue(df['off2'].dtype == 'timedelta64[ns]')
37593760

3761+
def test_datetimelike_setitem_with_inference(self):
3762+
if _np_version_under1p7:
3763+
raise nose.SkipTest("numpy < 1.7")
3764+
3765+
# GH 7592
3766+
# assignment of timedeltas with NaT
3767+
3768+
one_hour = timedelta(hours=1)
3769+
df = DataFrame(index=date_range('20130101',periods=4))
3770+
df['A'] = np.array([1*one_hour]*4, dtype='m8[ns]')
3771+
df.loc[:,'B'] = np.array([2*one_hour]*4, dtype='m8[ns]')
3772+
df.loc[:3,'C'] = np.array([3*one_hour]*3, dtype='m8[ns]')
3773+
df.ix[:,'D'] = np.array([4*one_hour]*4, dtype='m8[ns]')
3774+
df.ix[:3,'E'] = np.array([5*one_hour]*3, dtype='m8[ns]')
3775+
df['F'] = np.timedelta64('NaT')
3776+
df.ix[:-1,'F'] = np.array([6*one_hour]*3, dtype='m8[ns]')
3777+
df.ix[-3:,'G'] = date_range('20130101',periods=3)
3778+
df['H'] = np.datetime64('NaT')
3779+
result = df.dtypes
3780+
expected = Series([np.dtype('timedelta64[ns]')]*6+[np.dtype('datetime64[ns]')]*2,index=list('ABCDEFGH'))
3781+
assert_series_equal(result,expected)
3782+
37603783
def test_new_empty_index(self):
37613784
df1 = DataFrame(randn(0, 3))
37623785
df2 = DataFrame(randn(0, 3))

0 commit comments

Comments
 (0)