Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

ENH: added basic DatetimeBlock to internals

  • Loading branch information...
commit b9013354ad1e287bb265185f6e6e3f8a26e4df43 1 parent 65a1342
Adam Klein authored
6 pandas/core/frame.py
@@ -2064,9 +2064,9 @@ def reset_index(self, drop=False):
2064 2064 def _maybe_cast(values):
2065 2065 if values.dtype == np.object_:
2066 2066 values = lib.maybe_convert_objects(values)
2067   - elif values.dtype == np.datetime64:
2068   - # converts to datetime
2069   - values = values.astype('O')
  2067 + #elif values.dtype == np.datetime64:
  2068 + # # converts to datetime
  2069 + # values = values.astype('O')
2070 2070 return values
2071 2071
2072 2072 if not drop:
8 pandas/core/internals.py
@@ -281,12 +281,17 @@ def should_store(self, value):
281 281 return not issubclass(value.dtype.type,
282 282 (np.integer, np.floating, np.bool_))
283 283
  284 +class DatetimeBlock(IntBlock):
  285 + pass
  286 +
284 287 def make_block(values, items, ref_items, do_integrity_check=False):
285 288 dtype = values.dtype
286 289 vtype = dtype.type
287 290
288 291 if issubclass(vtype, np.floating):
289 292 klass = FloatBlock
  293 + elif issubclass(vtype, np.datetime64):
  294 + klass = DatetimeBlock
290 295 elif issubclass(vtype, np.integer):
291 296 if vtype != np.int64:
292 297 values = values.astype('i8')
@@ -1090,6 +1095,7 @@ def _interleaved_dtype(blocks):
1090 1095 have_bool = counts[BoolBlock] > 0
1091 1096 have_object = counts[ObjectBlock] > 0
1092 1097 have_float = counts[FloatBlock] > 0
  1098 + have_dt64 = counts[DatetimeBlock] > 0
1093 1099 have_numeric = have_float or have_int
1094 1100
1095 1101 if have_object:
@@ -1100,6 +1106,8 @@ def _interleaved_dtype(blocks):
1100 1106 return np.bool_
1101 1107 elif have_int and not have_float:
1102 1108 return np.int64
  1109 + elif have_dt64 and not have_float:
  1110 + return np.datetime64
1103 1111 else:
1104 1112 return np.float64
1105 1113
10 pandas/tests/test_datetime64.py
@@ -16,7 +16,7 @@
16 16
17 17 from numpy.random import rand
18 18
19   -from pandas.util.testing import assert_series_equal
  19 +from pandas.util.testing import assert_series_equal, assert_frame_equal
20 20
21 21 from pandas.core.groupby import Tinterval
22 22 from pandas.core.datetools import Minute, BDay
@@ -586,6 +586,14 @@ def test_dti_snap(self):
586 586
587 587 self.assert_( (res == exp).all() )
588 588
  589 + def test_dti_reset_index_round_trip(self):
  590 + dti = DatetimeIndex(start='1/1/2001', end='6/1/2001', freq='D')
  591 + d1 = DataFrame({'v' : np.random.rand(len(dti))}, index=dti)
  592 + d2 = d1.reset_index()
  593 + self.assert_(d2.dtypes[0] == np.datetime64)
  594 + d3 = d2.set_index('index')
  595 + assert_frame_equal(d1, d3)
  596 +
589 597 if __name__ == '__main__':
590 598 import nose
591 599 nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],

0 comments on commit b901335

Please sign in to comment.
Something went wrong with that request. Please try again.