Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

ENH: added basic DatetimeBlock to internals

  • Loading branch information...
commit b9013354ad1e287bb265185f6e6e3f8a26e4df43 1 parent 65a1342
Adam Klein authored
6 pandas/core/frame.py
View
@@ -2064,9 +2064,9 @@ def reset_index(self, drop=False):
def _maybe_cast(values):
if values.dtype == np.object_:
values = lib.maybe_convert_objects(values)
- elif values.dtype == np.datetime64:
- # converts to datetime
- values = values.astype('O')
+ #elif values.dtype == np.datetime64:
+ # # converts to datetime
+ # values = values.astype('O')
return values
if not drop:
8 pandas/core/internals.py
View
@@ -281,12 +281,17 @@ def should_store(self, value):
return not issubclass(value.dtype.type,
(np.integer, np.floating, np.bool_))
+class DatetimeBlock(IntBlock):
+ pass
+
def make_block(values, items, ref_items, do_integrity_check=False):
dtype = values.dtype
vtype = dtype.type
if issubclass(vtype, np.floating):
klass = FloatBlock
+ elif issubclass(vtype, np.datetime64):
+ klass = DatetimeBlock
elif issubclass(vtype, np.integer):
if vtype != np.int64:
values = values.astype('i8')
@@ -1090,6 +1095,7 @@ def _interleaved_dtype(blocks):
have_bool = counts[BoolBlock] > 0
have_object = counts[ObjectBlock] > 0
have_float = counts[FloatBlock] > 0
+ have_dt64 = counts[DatetimeBlock] > 0
have_numeric = have_float or have_int
if have_object:
@@ -1100,6 +1106,8 @@ def _interleaved_dtype(blocks):
return np.bool_
elif have_int and not have_float:
return np.int64
+ elif have_dt64 and not have_float:
+ return np.datetime64
else:
return np.float64
10 pandas/tests/test_datetime64.py
View
@@ -16,7 +16,7 @@
from numpy.random import rand
-from pandas.util.testing import assert_series_equal
+from pandas.util.testing import assert_series_equal, assert_frame_equal
from pandas.core.groupby import Tinterval
from pandas.core.datetools import Minute, BDay
@@ -586,6 +586,14 @@ def test_dti_snap(self):
self.assert_( (res == exp).all() )
+ def test_dti_reset_index_round_trip(self):
+ dti = DatetimeIndex(start='1/1/2001', end='6/1/2001', freq='D')
+ d1 = DataFrame({'v' : np.random.rand(len(dti))}, index=dti)
+ d2 = d1.reset_index()
+ self.assert_(d2.dtypes[0] == np.datetime64)
+ d3 = d2.set_index('index')
+ assert_frame_equal(d1, d3)
+
if __name__ == '__main__':
import nose
nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],
Please sign in to comment.
Something went wrong with that request. Please try again.