Skip to content

Commit 20f4527

Browse files
committed
ENH: handle datetime64 in block formation from dict of arrays in DataFrame constructor, close #1037
1 parent dbf71f9 commit 20f4527

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

pandas/core/internals.py

+9
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ def should_store(self, value):
286286
class DatetimeBlock(IntBlock):
287287
pass
288288

289+
289290
def make_block(values, items, ref_items, do_integrity_check=False):
290291
dtype = values.dtype
291292
vtype = dtype.type
@@ -308,6 +309,7 @@ def make_block(values, items, ref_items, do_integrity_check=False):
308309

309310
# TODO: flexible with index=None and/or items=None
310311

312+
311313
class BlockManager(object):
312314
"""
313315
Core internal data structure to implement DataFrame
@@ -1002,9 +1004,12 @@ def form_blocks(data, axes):
10021004
int_dict = {}
10031005
bool_dict = {}
10041006
object_dict = {}
1007+
datetime_dict = {}
10051008
for k, v in data.iteritems():
10061009
if issubclass(v.dtype.type, np.floating):
10071010
float_dict[k] = v
1011+
elif issubclass(v.dtype.type, np.datetime64):
1012+
datetime_dict[k] = v
10081013
elif issubclass(v.dtype.type, np.integer):
10091014
int_dict[k] = v
10101015
elif v.dtype == np.bool_:
@@ -1021,6 +1026,10 @@ def form_blocks(data, axes):
10211026
int_block = _simple_blockify(int_dict, items, np.int64)
10221027
blocks.append(int_block)
10231028

1029+
if len(datetime_dict):
1030+
datetime_block = _simple_blockify(datetime_dict, items, np.datetime64)
1031+
blocks.append(datetime_block)
1032+
10241033
if len(bool_dict):
10251034
bool_block = _simple_blockify(bool_dict, items, np.bool_)
10261035
blocks.append(bool_block)

pandas/tests/test_timeseries.py

+16
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,22 @@ def _ohlc(group):
270270
exp = _ohlc(ts['1/1/2000 5:55:01':])
271271
self.assert_((converted.ix['1/1/2000 6:00:00'] == exp).all())
272272

273+
def test_frame_ctor_datetime64_column(self):
274+
rng = date_range('1/1/2000 00:00:00', '1/1/2000 1:59:50',
275+
freq='10s')
276+
dates = np.asarray(rng)
277+
278+
df = DataFrame({'A': np.random.randn(len(rng)), 'B': dates})
279+
self.assert_(np.issubdtype(df['B'].dtype, np.datetime64))
280+
281+
def test_series_ctor_datetime64(self):
282+
rng = date_range('1/1/2000 00:00:00', '1/1/2000 1:59:50',
283+
freq='10s')
284+
dates = np.asarray(rng)
285+
286+
series = Series(dates)
287+
self.assert_(np.issubdtype(series.dtype, np.datetime64))
288+
273289

274290
def _skip_if_no_pytz():
275291
try:

0 commit comments

Comments
 (0)