Skip to content

Commit e91a091

Browse files
committed
Merge pull request #6530 from jreback/datetime_convert
BUG: Regression from 0.13 in the treatmenet of numpy datetime64 non-ns dtypes in Series creation (GH6429)
2 parents 47785e3 + 7b0d5cc commit e91a091

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ Bug Fixes
195195
fail (:issue:`6445`).
196196
- Bug in multi-axis indexing using ``.loc`` on non-unique indices (:issue:`6504`)
197197
- Bug that caused _ref_locs corruption when slice indexing across columns axis of a DataFrame (:issue:`6525`)
198+
- Regression from 0.13 in the treatmenet of numpy ``datetime64`` non-ns dtypes in Series creation (:issue:`6529`)
198199

199200
pandas 0.13.1
200201
-------------

pandas/core/common.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def isnull(obj):
124124
125125
See also
126126
--------
127-
pandas.notnull: boolean inverse of pandas.isnull
127+
pandas.notnull: boolean inverse of pandas.isnull
128128
"""
129129
return _isnull(obj)
130130

@@ -272,7 +272,7 @@ def notnull(obj):
272272
isnulled : array-like of bool or bool
273273
Array or bool indicating whether an object is *not* null or if an array
274274
is given which of the element is *not* null.
275-
275+
276276
See also
277277
--------
278278
pandas.isnull : boolean inverse of pandas.notnull
@@ -1727,10 +1727,7 @@ def _possibly_cast_to_datetime(value, dtype, coerce=False):
17271727
dtype = value.dtype
17281728

17291729
if dtype.kind == 'M' and dtype != _NS_DTYPE:
1730-
try:
1731-
value = tslib.array_to_datetime(value)
1732-
except:
1733-
raise
1730+
value = value.astype(_NS_DTYPE)
17341731

17351732
elif dtype.kind == 'm' and dtype != _TD_DTYPE:
17361733
from pandas.tseries.timedeltas import \

pandas/tests/test_series.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,25 @@ def test_constructor_dtype_datetime64(self):
600600
self.assertEqual(result['a'], Timestamp('20130101'))
601601
self.assertEqual(result['b'], 1)
602602

603+
# GH6529
604+
# coerce datetime64 non-ns properly
605+
dates = date_range('01-Jan-2015', '01-Dec-2015', freq='M')
606+
values2 = dates.view(np.ndarray).astype('datetime64[ns]')
607+
expected = Series(values2, dates)
608+
609+
# numpy < 1.7 is very odd about astyping
610+
if not _np_version_under1p7:
611+
for dtype in ['s','D','ms','us','ns']:
612+
values1 = dates.view(np.ndarray).astype('M8[{0}]'.format(dtype))
613+
result = Series(values1, dates)
614+
assert_series_equal(result,expected)
615+
616+
# leave datetime.date alone
617+
dates2 = np.array([ d.date() for d in dates.to_pydatetime() ],dtype=object)
618+
series1 = Series(dates2, dates)
619+
self.assert_numpy_array_equal(series1.values,dates2)
620+
self.assertEqual(series1.dtype,object)
621+
603622
def test_constructor_dict(self):
604623
d = {'a': 0., 'b': 1., 'c': 2.}
605624
result = Series(d, index=['b', 'c', 'd', 'a'])

0 commit comments

Comments
 (0)