Skip to content

Commit abb2df5

Browse files
committed
BUG: (GH10408, GH10412) in vectorised setting of timestamp columns
Fix setting values with python datetime.date and numpy datetime64.
1 parent ebea3a3 commit abb2df5

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

doc/source/whatsnew/v0.17.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -393,5 +393,6 @@ Bug Fixes
393393
- Bug in operator equal on Index not being consistent with Series (:issue:`9947`)
394394
- Reading "famafrench" data via ``DataReader`` results in HTTP 404 error because of the website url is changed (:issue:`10591`).
395395
- Bug in `read_msgpack` where DataFrame to decode has duplicate column names (:issue:`9618`)
396-
397396
- Bug in ``io.common.get_filepath_or_buffer`` which caused reading of valid S3 files to fail if the bucket also contained keys for which the user does not have read permission (:issue:`10604`)
397+
- Bug in vectorised setting of timestamp columns with python ``datetime.date`` and numpy ``datetime64`` (:issue:`10408`, :issue:`10412`)
398+

pandas/core/internals.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import itertools
33
import re
44
import operator
5-
from datetime import datetime, timedelta
5+
from datetime import datetime, timedelta, date
66
from collections import defaultdict
77

88
import numpy as np
@@ -1839,7 +1839,7 @@ def _try_coerce_args(self, values, other):
18391839

18401840
if is_null_datelike_scalar(other):
18411841
other = tslib.iNaT
1842-
elif isinstance(other, datetime):
1842+
elif isinstance(other, (datetime, np.datetime64, date)):
18431843
other = lib.Timestamp(other).asm8.view('i8')
18441844
elif hasattr(other, 'dtype') and com.is_integer_dtype(other):
18451845
other = other.view('i8')

pandas/tests/test_frame.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import print_function
44
# pylint: disable-msg=W0612,E1101
55
from copy import deepcopy
6-
from datetime import datetime, timedelta, time
6+
from datetime import datetime, timedelta, time, date
77
import sys
88
import operator
99
import re
@@ -4248,6 +4248,16 @@ def test_datetimelike_setitem_with_inference(self):
42484248
expected = Series([np.dtype('timedelta64[ns]')]*6+[np.dtype('datetime64[ns]')]*2,index=list('ABCDEFGH'))
42494249
assert_series_equal(result,expected)
42504250

4251+
def test_setitem_datetime_coercion(self):
4252+
# GH 1048
4253+
df = pd.DataFrame({'c': [pd.Timestamp('2010-10-01')]*3})
4254+
df.loc[0:1, 'c'] = np.datetime64('2008-08-08')
4255+
self.assertEqual(pd.Timestamp('2008-08-08'), df.loc[0, 'c'])
4256+
self.assertEqual(pd.Timestamp('2008-08-08'), df.loc[1, 'c'])
4257+
df.loc[2, 'c'] = date(2005, 5, 5)
4258+
self.assertEqual(pd.Timestamp('2005-05-05'), df.loc[2, 'c'])
4259+
4260+
42514261
def test_new_empty_index(self):
42524262
df1 = DataFrame(randn(0, 3))
42534263
df2 = DataFrame(randn(0, 3))

pandas/tests/test_internals.py

+22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# -*- coding: utf-8 -*-
22
# pylint: disable=W0102
33

4+
from datetime import datetime, date
5+
46
import nose
57
import numpy as np
68

@@ -286,6 +288,26 @@ def test_repr(self):
286288
pass
287289

288290

291+
class TestDatetimeBlock(tm.TestCase):
292+
_multiprocess_can_split_ = True
293+
294+
def test_try_coerce_arg(self):
295+
block = create_block('datetime', [0])
296+
297+
# coerce None
298+
none_coerced = block._try_coerce_args(block.values, None)[1]
299+
self.assertTrue(pd.Timestamp(none_coerced) is pd.NaT)
300+
301+
# coerce different types of date bojects
302+
vals = (np.datetime64('2010-10-10'),
303+
datetime(2010, 10, 10),
304+
date(2010, 10, 10))
305+
for val in vals:
306+
coerced = block._try_coerce_args(block.values, val)[1]
307+
self.assertEqual(np.int64, type(coerced))
308+
self.assertEqual(pd.Timestamp('2010-10-10'), pd.Timestamp(coerced))
309+
310+
289311
class TestBlockManager(tm.TestCase):
290312
_multiprocess_can_split_ = True
291313

0 commit comments

Comments
 (0)