7
7
import numpy as np
8
8
import pandas as pd
9
9
10
- from pandas import (Index , Series , DataFrame , isnull , notnull ,
10
+ from pandas import (Index , Series , DataFrame , Timestamp , isnull , notnull ,
11
11
bdate_range , date_range , _np_version_under1p7 )
12
12
import pandas .core .common as com
13
13
from pandas .compat import StringIO , lrange , range , zip , u , OrderedDict , long
@@ -123,8 +123,8 @@ def conv(v):
123
123
def test_nat_converters (self ):
124
124
_skip_if_numpy_not_friendly ()
125
125
126
- self .assert_ (to_timedelta ('nat' ) == tslib .iNaT )
127
- self .assert_ (to_timedelta ('nan' ) == tslib .iNaT )
126
+ self .assert_ (to_timedelta ('nat' , box = False ) == tslib .iNaT )
127
+ self .assert_ (to_timedelta ('nan' , box = False ) == tslib .iNaT )
128
128
129
129
def test_to_timedelta (self ):
130
130
_skip_if_numpy_not_friendly ()
@@ -133,11 +133,11 @@ def conv(v):
133
133
return v .astype ('m8[ns]' )
134
134
d1 = np .timedelta64 (1 ,'D' )
135
135
136
- self .assert_ (to_timedelta ('1 days 06:05:01.00003' ) == conv (d1 + np .timedelta64 (6 * 3600 + 5 * 60 + 1 ,'s' )+ np .timedelta64 (30 ,'us' )))
137
- self .assert_ (to_timedelta ('15.5us' ) == conv (np .timedelta64 (15500 ,'ns' )))
136
+ self .assert_ (to_timedelta ('1 days 06:05:01.00003' , box = False ) == conv (d1 + np .timedelta64 (6 * 3600 + 5 * 60 + 1 ,'s' )+ np .timedelta64 (30 ,'us' )))
137
+ self .assert_ (to_timedelta ('15.5us' , box = False ) == conv (np .timedelta64 (15500 ,'ns' )))
138
138
139
139
# empty string
140
- result = to_timedelta ('' )
140
+ result = to_timedelta ('' , box = False )
141
141
self .assert_ (result == tslib .iNaT )
142
142
143
143
result = to_timedelta (['' , '' ])
@@ -150,7 +150,7 @@ def conv(v):
150
150
151
151
# ints
152
152
result = np .timedelta64 (0 ,'ns' )
153
- expected = to_timedelta (0 )
153
+ expected = to_timedelta (0 , box = False )
154
154
self .assert_ (result == expected )
155
155
156
156
# Series
@@ -163,6 +163,35 @@ def conv(v):
163
163
expected = to_timedelta ([0 ,10 ],unit = 's' )
164
164
tm .assert_series_equal (result , expected )
165
165
166
+ # single element conversion
167
+ v = timedelta (seconds = 1 )
168
+ result = to_timedelta (v ,box = False )
169
+ expected = to_timedelta ([v ])
170
+
171
+ v = np .timedelta64 (timedelta (seconds = 1 ))
172
+ result = to_timedelta (v ,box = False )
173
+ expected = to_timedelta ([v ])
174
+
175
+ def test_timedelta_ops (self ):
176
+ _skip_if_numpy_not_friendly ()
177
+
178
+ # GH4984
179
+ # make sure ops return timedeltas
180
+ s = Series ([Timestamp ('20130101' ) + timedelta (seconds = i * i ) for i in range (10 ) ])
181
+ td = s .diff ()
182
+
183
+ result = td .mean ()
184
+ expected = to_timedelta (timedelta (seconds = 9 ))
185
+ tm .assert_series_equal (result , expected )
186
+
187
+ result = td .quantile (.1 )
188
+ expected = to_timedelta ('00:00:02.6' )
189
+ tm .assert_series_equal (result , expected )
190
+
191
+ result = td .median ()
192
+ expected = to_timedelta ('00:00:08' )
193
+ tm .assert_series_equal (result , expected )
194
+
166
195
if __name__ == '__main__' :
167
196
nose .runmodule (argv = [__file__ , '-vvs' , '-x' , '--pdb' , '--pdb-failure' ],
168
197
exit = False )
0 commit comments