|
38 | 38 | import pandas.util.py3compat as py3compat
|
39 | 39 | from pandas.core.datetools import BDay
|
40 | 40 | import pandas.core.common as com
|
| 41 | +from pandas import concat |
41 | 42 |
|
42 | 43 | from numpy.testing.decorators import slow
|
43 | 44 |
|
@@ -171,7 +172,6 @@ def test_indexing_over_size_cutoff(self):
|
171 | 172 | def test_indexing_unordered(self):
|
172 | 173 |
|
173 | 174 | # GH 2437
|
174 |
| - from pandas import concat |
175 | 175 | rng = date_range(start='2011-01-01', end='2011-01-15')
|
176 | 176 | ts = Series(randn(len(rng)), index=rng)
|
177 | 177 | ts2 = concat([ts[0:4],ts[-4:],ts[4:-4]])
|
@@ -593,6 +593,34 @@ def test_frame_add_datetime64_col_other_units(self):
|
593 | 593 |
|
594 | 594 | self.assert_((tmp['dates'].values == ex_vals).all())
|
595 | 595 |
|
| 596 | + def test_to_datetime_unit(self): |
| 597 | + |
| 598 | + epoch = 1370745748 |
| 599 | + s = Series([ epoch + t for t in range(20) ]) |
| 600 | + result = to_datetime(s,unit='s') |
| 601 | + expected = Series([ Timestamp('2013-06-09 02:42:28') + timedelta(seconds=t) for t in range(20) ]) |
| 602 | + assert_series_equal(result,expected) |
| 603 | + |
| 604 | + s = Series([ epoch + t for t in range(20) ]).astype(float) |
| 605 | + result = to_datetime(s,unit='s') |
| 606 | + expected = Series([ Timestamp('2013-06-09 02:42:28') + timedelta(seconds=t) for t in range(20) ]) |
| 607 | + assert_series_equal(result,expected) |
| 608 | + |
| 609 | + s = Series([ epoch + t for t in range(20) ] + [iNaT]) |
| 610 | + result = to_datetime(s,unit='s') |
| 611 | + expected = Series([ Timestamp('2013-06-09 02:42:28') + timedelta(seconds=t) for t in range(20) ] + [NaT]) |
| 612 | + assert_series_equal(result,expected) |
| 613 | + |
| 614 | + s = Series([ epoch + t for t in range(20) ] + [iNaT]).astype(float) |
| 615 | + result = to_datetime(s,unit='s') |
| 616 | + expected = Series([ Timestamp('2013-06-09 02:42:28') + timedelta(seconds=t) for t in range(20) ] + [NaT]) |
| 617 | + assert_series_equal(result,expected) |
| 618 | + |
| 619 | + s = concat([Series([ epoch + t for t in range(20) ]).astype(float),Series([np.nan])],ignore_index=True) |
| 620 | + result = to_datetime(s,unit='s') |
| 621 | + expected = Series([ Timestamp('2013-06-09 02:42:28') + timedelta(seconds=t) for t in range(20) ] + [NaT]) |
| 622 | + assert_series_equal(result,expected) |
| 623 | + |
596 | 624 | def test_series_ctor_datetime64(self):
|
597 | 625 | rng = date_range('1/1/2000 00:00:00', '1/1/2000 1:59:50',
|
598 | 626 | freq='10s')
|
@@ -2691,6 +2719,61 @@ def test_basics_nanos(self):
|
2691 | 2719 | self.assert_(stamp.microsecond == 0)
|
2692 | 2720 | self.assert_(stamp.nanosecond == 500)
|
2693 | 2721 |
|
| 2722 | + def test_unit(self): |
| 2723 | + def check(val,unit=None,s=1,us=0): |
| 2724 | + stamp = Timestamp(val, unit=unit) |
| 2725 | + self.assert_(stamp.year == 2000) |
| 2726 | + self.assert_(stamp.month == 1) |
| 2727 | + self.assert_(stamp.day == 1) |
| 2728 | + self.assert_(stamp.hour == 1) |
| 2729 | + self.assert_(stamp.minute == 1) |
| 2730 | + self.assert_(stamp.second == s) |
| 2731 | + self.assert_(stamp.microsecond == us) |
| 2732 | + self.assert_(stamp.nanosecond == 0) |
| 2733 | + |
| 2734 | + val = Timestamp('20000101 01:01:01').value |
| 2735 | + |
| 2736 | + check(val) |
| 2737 | + check(val/1000L,unit='us') |
| 2738 | + check(val/1000000L,unit='ms') |
| 2739 | + check(val/1000000000L,unit='s') |
| 2740 | + |
| 2741 | + # using truediv, so these are like floats |
| 2742 | + if py3compat.PY3: |
| 2743 | + check((val+500000)/1000000000L,unit='s',us=500) |
| 2744 | + check((val+500000000)/1000000000L,unit='s',us=500000) |
| 2745 | + check((val+500000)/1000000L,unit='ms',us=500) |
| 2746 | + |
| 2747 | + # get chopped in py2 |
| 2748 | + else: |
| 2749 | + check((val+500000)/1000000000L,unit='s') |
| 2750 | + check((val+500000000)/1000000000L,unit='s') |
| 2751 | + check((val+500000)/1000000L,unit='ms') |
| 2752 | + |
| 2753 | + # ok |
| 2754 | + check((val+500000)/1000L,unit='us',us=500) |
| 2755 | + check((val+500000000)/1000000L,unit='ms',us=500000) |
| 2756 | + |
| 2757 | + # floats |
| 2758 | + check(val/1000.0 + 5,unit='us',us=5) |
| 2759 | + check(val/1000.0 + 5000,unit='us',us=5000) |
| 2760 | + check(val/1000000.0 + 0.5,unit='ms',us=500) |
| 2761 | + check(val/1000000.0 + 0.005,unit='ms',us=5) |
| 2762 | + check(val/1000000000.0 + 0.5,unit='s',us=500000) |
| 2763 | + |
| 2764 | + # nan |
| 2765 | + result = Timestamp(np.nan) |
| 2766 | + self.assert_(result is NaT) |
| 2767 | + |
| 2768 | + result = Timestamp(None) |
| 2769 | + self.assert_(result is NaT) |
| 2770 | + |
| 2771 | + result = Timestamp(iNaT) |
| 2772 | + self.assert_(result is NaT) |
| 2773 | + |
| 2774 | + result = Timestamp(NaT) |
| 2775 | + self.assert_(result is NaT) |
| 2776 | + |
2694 | 2777 | def test_comparison(self):
|
2695 | 2778 | # 5-18-2012 00:00:00.000
|
2696 | 2779 | stamp = 1337299200000000000L
|
|
0 commit comments