Skip to content

Commit d7c6801

Browse files
committed
Merge pull request #6028 from jreback/series_infer
BUG: Bug in Series construction of mixed type with datelike and an integer
2 parents 08c1302 + 409906e commit d7c6801

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

doc/source/release.rst

+2
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ Bug Fixes
130130
and off-diagonal plots, see (:issue:`5497`).
131131
- Regression in Series with a multi-index via ix (:issue:`6018`)
132132
- Bug in Series.xs with a multi-index (:issue:`6018`)
133+
- Bug in Series construction of mixed type with datelike and an integer (which should result in
134+
object type and not automatic conversion) (:issue:`6028`)
133135

134136
pandas 0.13.0
135137
-------------

pandas/tests/test_indexing.py

+6
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,12 @@ def test_loc_general(self):
623623
self.assert_((result.columns == ['A','B']).all() == True)
624624
self.assert_((result.index == ['A','B']).all() == True)
625625

626+
# mixed type
627+
result = DataFrame({ 'a' : [Timestamp('20130101')], 'b' : [1] }).iloc[0]
628+
expected = Series([ Timestamp('20130101'), 1],index=['a','b'])
629+
assert_series_equal(result,expected)
630+
self.assert_(result.dtype == object)
631+
626632
def test_loc_setitem_frame(self):
627633
df = self.frame_labels
628634

pandas/tests/test_series.py

+5
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,11 @@ def test_constructor_dtype_datetime64(self):
573573
result = Series([datetime(3000,1,1)])
574574
self.assert_(result[0] == datetime(3000,1,1,0,0))
575575

576+
# don't mix types
577+
result = Series([ Timestamp('20130101'), 1],index=['a','b'])
578+
self.assert_(result['a'] == Timestamp('20130101'))
579+
self.assert_(result['b'] == 1)
580+
576581
def test_constructor_dict(self):
577582
d = {'a': 0., 'b': 1., 'c': 2.}
578583
result = Series(d, index=['b', 'c', 'd', 'a'])

pandas/tslib.pyx

+12-1
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
10051005
ndarray[int64_t] iresult
10061006
ndarray[object] oresult
10071007
pandas_datetimestruct dts
1008-
bint utc_convert = bool(utc)
1008+
bint utc_convert = bool(utc), seen_integer=0, seen_datetime=0
10091009
_TSObject _ts
10101010
int64_t m = cast_from_unit(None,unit)
10111011

@@ -1017,6 +1017,7 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
10171017
if _checknull_with_nat(val):
10181018
iresult[i] = iNaT
10191019
elif PyDateTime_Check(val):
1020+
seen_datetime=1
10201021
if val.tzinfo is not None:
10211022
if utc_convert:
10221023
_ts = convert_to_tsobject(val, None, unit)
@@ -1047,6 +1048,7 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
10471048
iresult[i] = _date_to_datetime64(val, &dts)
10481049
try:
10491050
_check_dts_bounds(&dts)
1051+
seen_datetime=1
10501052
except ValueError:
10511053
if coerce:
10521054
iresult[i] = iNaT
@@ -1058,6 +1060,7 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
10581060
else:
10591061
try:
10601062
iresult[i] = _get_datetime64_nanos(val)
1063+
seen_datetime=1
10611064
except ValueError:
10621065
if coerce:
10631066
iresult[i] = iNaT
@@ -1070,11 +1073,13 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
10701073
iresult[i] = iNaT
10711074
else:
10721075
iresult[i] = val*m
1076+
seen_integer=1
10731077
elif util.is_float_object(val) and not coerce:
10741078
if val != val or val == iNaT:
10751079
iresult[i] = iNaT
10761080
else:
10771081
iresult[i] = cast_from_unit(val,unit)
1082+
seen_integer=1
10781083
else:
10791084
try:
10801085
if len(val) == 0:
@@ -1114,6 +1119,12 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
11141119
continue
11151120
raise
11161121

1122+
# don't allow mixed integers and datetime like
1123+
# higher levels can catch and coerce to object, for
1124+
# example
1125+
if seen_integer and seen_datetime:
1126+
raise ValueError("mixed datetimes and integers in passed array")
1127+
11171128
return result
11181129
except OutOfBoundsDatetime:
11191130
if raise_:

0 commit comments

Comments
 (0)