|
19 | 19 | assert_almost_equal,
|
20 | 20 | assert_index_equal,
|
21 | 21 | ensure_clean)
|
| 22 | +from numpy.testing import assert_allclose |
22 | 23 | from pandas.tseries.offsets import Day, Second, Hour
|
23 | 24 | import pandas.util.testing as tm
|
24 | 25 | from numpy.random import rand, randn
|
@@ -953,6 +954,36 @@ def test_fields(self):
|
953 | 954 | tm.assert_series_equal(s.dt.days,Series([1,np.nan],index=[0,1]))
|
954 | 955 | tm.assert_series_equal(s.dt.seconds,Series([10*3600+11*60+12,np.nan],index=[0,1]))
|
955 | 956 |
|
| 957 | + def test_total_seconds(self): |
| 958 | + # GH 10939 |
| 959 | + # test index |
| 960 | + rng = timedelta_range('1 days, 10:11:12.100123456', periods=2, freq='s') |
| 961 | + expt = [1*86400+10*3600+11*60+12+100123456./1e9,1*86400+10*3600+11*60+13+100123456./1e9] |
| 962 | + assert_allclose(rng.total_seconds(), expt, atol=1e-10, rtol=0) |
| 963 | + |
| 964 | + # test Series |
| 965 | + s = Series(rng) |
| 966 | + s_expt = Series(expt,index=[0,1]) |
| 967 | + tm.assert_series_equal(s.dt.total_seconds(),s_expt) |
| 968 | + |
| 969 | + # with nat |
| 970 | + s[1] = np.nan |
| 971 | + s_expt = Series([1*86400+10*3600+11*60+12+100123456./1e9,np.nan],index=[0,1]) |
| 972 | + tm.assert_series_equal(s.dt.total_seconds(),s_expt) |
| 973 | + |
| 974 | + # with both nat |
| 975 | + s = Series([np.nan,np.nan], dtype='timedelta64[ns]') |
| 976 | + tm.assert_series_equal(s.dt.total_seconds(),Series([np.nan,np.nan],index=[0,1])) |
| 977 | + |
| 978 | + def test_total_seconds_scalar(self): |
| 979 | + # GH 10939 |
| 980 | + rng = Timedelta('1 days, 10:11:12.100123456') |
| 981 | + expt = 1*86400+10*3600+11*60+12+100123456./1e9 |
| 982 | + assert_allclose(rng.total_seconds(), expt, atol=1e-10, rtol=0) |
| 983 | + |
| 984 | + rng = Timedelta(np.nan) |
| 985 | + self.assertTrue(np.isnan(rng.total_seconds())) |
| 986 | + |
956 | 987 | def test_components(self):
|
957 | 988 | rng = timedelta_range('1 days, 10:11:12', periods=2, freq='s')
|
958 | 989 | rng.components
|
|
0 commit comments