Skip to content

BUG: Bug in Series construction of mixed type with datelike and an integer #6028

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

Merged
merged 1 commit into from
Jan 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ Bug Fixes
and off-diagonal plots, see (:issue:`5497`).
- Regression in Series with a multi-index via ix (:issue:`6018`)
- Bug in Series.xs with a multi-index (:issue:`6018`)
- Bug in Series construction of mixed type with datelike and an integer (which should result in
object type and not automatic conversion) (:issue:`6028`)

pandas 0.13.0
-------------
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,12 @@ def test_loc_general(self):
self.assert_((result.columns == ['A','B']).all() == True)
self.assert_((result.index == ['A','B']).all() == True)

# mixed type
result = DataFrame({ 'a' : [Timestamp('20130101')], 'b' : [1] }).iloc[0]
expected = Series([ Timestamp('20130101'), 1],index=['a','b'])
assert_series_equal(result,expected)
self.assert_(result.dtype == object)

def test_loc_setitem_frame(self):
df = self.frame_labels

Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,11 @@ def test_constructor_dtype_datetime64(self):
result = Series([datetime(3000,1,1)])
self.assert_(result[0] == datetime(3000,1,1,0,0))

# don't mix types
result = Series([ Timestamp('20130101'), 1],index=['a','b'])
self.assert_(result['a'] == Timestamp('20130101'))
self.assert_(result['b'] == 1)

def test_constructor_dict(self):
d = {'a': 0., 'b': 1., 'c': 2.}
result = Series(d, index=['b', 'c', 'd', 'a'])
Expand Down
13 changes: 12 additions & 1 deletion pandas/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,7 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
ndarray[int64_t] iresult
ndarray[object] oresult
pandas_datetimestruct dts
bint utc_convert = bool(utc)
bint utc_convert = bool(utc), seen_integer=0, seen_datetime=0
_TSObject _ts
int64_t m = cast_from_unit(None,unit)

Expand All @@ -1017,6 +1017,7 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
if _checknull_with_nat(val):
iresult[i] = iNaT
elif PyDateTime_Check(val):
seen_datetime=1
if val.tzinfo is not None:
if utc_convert:
_ts = convert_to_tsobject(val, None, unit)
Expand Down Expand Up @@ -1047,6 +1048,7 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
iresult[i] = _date_to_datetime64(val, &dts)
try:
_check_dts_bounds(&dts)
seen_datetime=1
except ValueError:
if coerce:
iresult[i] = iNaT
Expand All @@ -1058,6 +1060,7 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
else:
try:
iresult[i] = _get_datetime64_nanos(val)
seen_datetime=1
except ValueError:
if coerce:
iresult[i] = iNaT
Expand All @@ -1070,11 +1073,13 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
iresult[i] = iNaT
else:
iresult[i] = val*m
seen_integer=1
elif util.is_float_object(val) and not coerce:
if val != val or val == iNaT:
iresult[i] = iNaT
else:
iresult[i] = cast_from_unit(val,unit)
seen_integer=1
else:
try:
if len(val) == 0:
Expand Down Expand Up @@ -1114,6 +1119,12 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
continue
raise

# don't allow mixed integers and datetime like
# higher levels can catch and coerce to object, for
# example
if seen_integer and seen_datetime:
raise ValueError("mixed datetimes and integers in passed array")

return result
except OutOfBoundsDatetime:
if raise_:
Expand Down