1
1
import numpy as np
2
2
3
+ import pandas as pd
3
4
import pandas .lib as lib
4
5
import pandas .util .testing as tm
5
- from pandas import Float64Index , date_range , Timestamp
6
6
from pandas import (Index , DatetimeIndex , datetime , offsets , to_datetime ,
7
- Series , DataFrame )
7
+ Series , DataFrame , Float64Index , date_range , Timestamp )
8
+
9
+ from pandas .util .testing import assert_series_equal
8
10
9
11
10
12
class TestDateTimeIndexToJulianDate (tm .TestCase ):
@@ -130,40 +132,6 @@ def test_range_edges(self):
130
132
'1970-01-03' , '1970-01-04' ])
131
133
tm .assert_index_equal (idx , exp )
132
134
133
- def test_date_range_businesshour (self ):
134
- idx = DatetimeIndex (['2014-07-04 09:00' , '2014-07-04 10:00' ,
135
- '2014-07-04 11:00' ,
136
- '2014-07-04 12:00' , '2014-07-04 13:00' ,
137
- '2014-07-04 14:00' ,
138
- '2014-07-04 15:00' , '2014-07-04 16:00' ],
139
- freq = 'BH' )
140
- rng = date_range ('2014-07-04 09:00' , '2014-07-04 16:00' , freq = 'BH' )
141
- tm .assert_index_equal (idx , rng )
142
-
143
- idx = DatetimeIndex (
144
- ['2014-07-04 16:00' , '2014-07-07 09:00' ], freq = 'BH' )
145
- rng = date_range ('2014-07-04 16:00' , '2014-07-07 09:00' , freq = 'BH' )
146
- tm .assert_index_equal (idx , rng )
147
-
148
- idx = DatetimeIndex (['2014-07-04 09:00' , '2014-07-04 10:00' ,
149
- '2014-07-04 11:00' ,
150
- '2014-07-04 12:00' , '2014-07-04 13:00' ,
151
- '2014-07-04 14:00' ,
152
- '2014-07-04 15:00' , '2014-07-04 16:00' ,
153
- '2014-07-07 09:00' , '2014-07-07 10:00' ,
154
- '2014-07-07 11:00' ,
155
- '2014-07-07 12:00' , '2014-07-07 13:00' ,
156
- '2014-07-07 14:00' ,
157
- '2014-07-07 15:00' , '2014-07-07 16:00' ,
158
- '2014-07-08 09:00' , '2014-07-08 10:00' ,
159
- '2014-07-08 11:00' ,
160
- '2014-07-08 12:00' , '2014-07-08 13:00' ,
161
- '2014-07-08 14:00' ,
162
- '2014-07-08 15:00' , '2014-07-08 16:00' ],
163
- freq = 'BH' )
164
- rng = date_range ('2014-07-04 09:00' , '2014-07-08 16:00' , freq = 'BH' )
165
- tm .assert_index_equal (idx , rng )
166
-
167
135
def test_datetimeindex_integers_shift (self ):
168
136
rng = date_range ('1/1/2000' , periods = 20 )
169
137
@@ -185,6 +153,110 @@ def test_datetimeindex_repr_short(self):
185
153
dr = date_range (start = '1/1/2012' , periods = 3 )
186
154
repr (dr )
187
155
156
+ def test_getitem_setitem_datetimeindex (self ):
157
+ N = 50
158
+ # testing with timezone, GH #2785
159
+ rng = date_range ('1/1/1990' , periods = N , freq = 'H' , tz = 'US/Eastern' )
160
+ ts = Series (np .random .randn (N ), index = rng )
161
+
162
+ result = ts ["1990-01-01 04:00:00" ]
163
+ expected = ts [4 ]
164
+ self .assertEqual (result , expected )
165
+
166
+ result = ts .copy ()
167
+ result ["1990-01-01 04:00:00" ] = 0
168
+ result ["1990-01-01 04:00:00" ] = ts [4 ]
169
+ assert_series_equal (result , ts )
170
+
171
+ result = ts ["1990-01-01 04:00:00" :"1990-01-01 07:00:00" ]
172
+ expected = ts [4 :8 ]
173
+ assert_series_equal (result , expected )
174
+
175
+ result = ts .copy ()
176
+ result ["1990-01-01 04:00:00" :"1990-01-01 07:00:00" ] = 0
177
+ result ["1990-01-01 04:00:00" :"1990-01-01 07:00:00" ] = ts [4 :8 ]
178
+ assert_series_equal (result , ts )
179
+
180
+ lb = "1990-01-01 04:00:00"
181
+ rb = "1990-01-01 07:00:00"
182
+ result = ts [(ts .index >= lb ) & (ts .index <= rb )]
183
+ expected = ts [4 :8 ]
184
+ assert_series_equal (result , expected )
185
+
186
+ # repeat all the above with naive datetimes
187
+ result = ts [datetime (1990 , 1 , 1 , 4 )]
188
+ expected = ts [4 ]
189
+ self .assertEqual (result , expected )
190
+
191
+ result = ts .copy ()
192
+ result [datetime (1990 , 1 , 1 , 4 )] = 0
193
+ result [datetime (1990 , 1 , 1 , 4 )] = ts [4 ]
194
+ assert_series_equal (result , ts )
195
+
196
+ result = ts [datetime (1990 , 1 , 1 , 4 ):datetime (1990 , 1 , 1 , 7 )]
197
+ expected = ts [4 :8 ]
198
+ assert_series_equal (result , expected )
199
+
200
+ result = ts .copy ()
201
+ result [datetime (1990 , 1 , 1 , 4 ):datetime (1990 , 1 , 1 , 7 )] = 0
202
+ result [datetime (1990 , 1 , 1 , 4 ):datetime (1990 , 1 , 1 , 7 )] = ts [4 :8 ]
203
+ assert_series_equal (result , ts )
204
+
205
+ lb = datetime (1990 , 1 , 1 , 4 )
206
+ rb = datetime (1990 , 1 , 1 , 7 )
207
+ result = ts [(ts .index >= lb ) & (ts .index <= rb )]
208
+ expected = ts [4 :8 ]
209
+ assert_series_equal (result , expected )
210
+
211
+ result = ts [ts .index [4 ]]
212
+ expected = ts [4 ]
213
+ self .assertEqual (result , expected )
214
+
215
+ result = ts [ts .index [4 :8 ]]
216
+ expected = ts [4 :8 ]
217
+ assert_series_equal (result , expected )
218
+
219
+ result = ts .copy ()
220
+ result [ts .index [4 :8 ]] = 0
221
+ result [4 :8 ] = ts [4 :8 ]
222
+ assert_series_equal (result , ts )
223
+
224
+ # also test partial date slicing
225
+ result = ts ["1990-01-02" ]
226
+ expected = ts [24 :48 ]
227
+ assert_series_equal (result , expected )
228
+
229
+ result = ts .copy ()
230
+ result ["1990-01-02" ] = 0
231
+ result ["1990-01-02" ] = ts [24 :48 ]
232
+ assert_series_equal (result , ts )
233
+
234
+ def test_normalize (self ):
235
+ rng = date_range ('1/1/2000 9:30' , periods = 10 , freq = 'D' )
236
+
237
+ result = rng .normalize ()
238
+ expected = date_range ('1/1/2000' , periods = 10 , freq = 'D' )
239
+ tm .assert_index_equal (result , expected )
240
+
241
+ rng_ns = pd .DatetimeIndex (np .array ([1380585623454345752 ,
242
+ 1380585612343234312 ]).astype (
243
+ "datetime64[ns]" ))
244
+ rng_ns_normalized = rng_ns .normalize ()
245
+ expected = pd .DatetimeIndex (np .array ([1380585600000000000 ,
246
+ 1380585600000000000 ]).astype (
247
+ "datetime64[ns]" ))
248
+ tm .assert_index_equal (rng_ns_normalized , expected )
249
+
250
+ self .assertTrue (result .is_normalized )
251
+ self .assertFalse (rng .is_normalized )
252
+
253
+ def test_series_ctor_plus_datetimeindex (self ):
254
+ rng = date_range ('20090415' , '20090519' , freq = 'B' )
255
+ data = dict ((k , 1 ) for k in rng )
256
+
257
+ result = Series (data , index = rng )
258
+ self .assertIs (result .index , rng )
259
+
188
260
189
261
class TestDatetime64 (tm .TestCase ):
190
262
0 commit comments