@@ -149,190 +149,106 @@ def test_normalize_max2one(data_in, expected):
149149 assert_allclose (result , expected )
150150
151151
152- @pytest .mark .parametrize (
153- 'input,expected' ,
154- [
155- (
156- {
157- "time" : datetime (
158- 1974 , 6 , 22 , 18 , 30 , 15 , tzinfo = ZoneInfo ("Etc/GMT+5" ),
159- ),
160- "location" : location .Location (
161- 43.19262774396091 , - 77.58782907414867 , tz = "Etc/GMT+5"
162- )
163- },
164- datetime (1974 , 6 , 22 , 23 , 30 , 15 , tzinfo = ZoneInfo ("UTC" )),
165- ),
166- (
167- {
168- "time" : datetime (1974 , 6 , 22 , 18 , 30 , 15 ),
169- "location" : location .Location (
170- 43.19262774396091 , - 77.58782907414867 , tz = "Etc/GMT+5"
171- )
172- },
173- datetime (1974 , 6 , 22 , 23 , 30 , 15 , tzinfo = ZoneInfo ("UTC" )),
174- ),
175- (
176- {
177- "time" : pd .DatetimeIndex (
178- ["1974-06-22T18:30:15" ],
179- tz = ZoneInfo ("Etc/GMT+5" ),
180- ),
181- "location" : location .Location (
182- 43.19262774396091 , - 77.58782907414867 , tz = "Etc/GMT+5"
183- )
184- },
185- pd .DatetimeIndex (["1974-06-22T23:30:15" ], tz = ZoneInfo ("UTC" )),
186- ),
187- (
188- {
189- "time" : pd .DatetimeIndex (["1974-06-22T18:30:15" ]),
190- "location" : location .Location (
191- 43.19262774396091 , - 77.58782907414867 , tz = "Etc/GMT+5"
192- )
193- },
194- pd .DatetimeIndex (["1974-06-22T23:30:15" ], tz = ZoneInfo ("UTC" )),
195- ),
196- (
197- {
198- "time" : pd .Series (
199- [24.42 ],
200- index = pd .DatetimeIndex (
201- ["1974-06-22T18:30:15" ],
202- tz = ZoneInfo ("Etc/GMT+5" ),
203- ),
204- ),
205- "location" : location .Location (
206- 43.19262774396091 , - 77.58782907414867 , tz = "Etc/GMT+5"
207- )
208- },
209- pd .Series (
210- [24.42 ],
211- pd .DatetimeIndex (["1974-06-22T23:30:15" ], tz = ZoneInfo ("UTC" )),
212- ),
213- ),
214- (
215- {
216- "time" : pd .Series (
217- [24.42 ],
218- index = pd .DatetimeIndex (["1974-06-22T18:30:15" ]),
219- ),
220- "location" : location .Location (
221- 43.19262774396091 , - 77.58782907414867 , tz = "Etc/GMT+5"
222- )
223- },
224- pd .Series (
225- [24.42 ],
226- pd .DatetimeIndex (["1974-06-22T23:30:15" ], tz = ZoneInfo ("UTC" )),
227- ),
228- ),
229- (
230- {
231- "time" : pd .DataFrame (
232- [[24.42 ]],
233- index = pd .DatetimeIndex (
234- ["1974-06-22T18:30:15" ],
235- tz = ZoneInfo ("Etc/GMT+5" ),
236- ),
237- ),
238- "location" : location .Location (
239- 43.19262774396091 , - 77.58782907414867 , tz = "Etc/GMT+5"
240- )
241- },
242- pd .DataFrame (
243- [[24.42 ]],
244- pd .DatetimeIndex (["1974-06-22T23:30:15" ], tz = ZoneInfo ("UTC" )),
245- ),
246- ),
247- (
248- {
249- "time" : pd .DataFrame (
250- [[24.42 ]],
251- index = pd .DatetimeIndex (["1974-06-22T18:30:15" ]),
252- ),
253- "location" : location .Location (
254- 43.19262774396091 , - 77.58782907414867 , tz = "Etc/GMT+5"
255- )
256- },
257- pd .DataFrame (
258- [[24.42 ]],
259- pd .DatetimeIndex (["1974-06-22T23:30:15" ], tz = ZoneInfo ("UTC" )),
260- ),
261- ),
262- ],
263- ids = [
264- "datetime.datetime with tzinfo" ,
265- "datetime.datetime" ,
266- "pandas.DatetimeIndex with tzinfo" ,
267- "pandas.DatetimeIndex" ,
268- "pandas.Series with tzinfo" ,
269- "pandas.Series" ,
270- "pandas.DataFrame with tzinfo" ,
271- "pandas.DataFrame" ,
272- ],
273- )
274- def test_localize_to_utc (input , expected ):
275- got = tools .localize_to_utc (** input )
276-
277- if isinstance (got , (pd .Series , pd .DataFrame )):
278- # Older pandas versions have wonky dtype equality check on timestamp
279- # index, so check the values as numpy.ndarray and indices one by one.
280- np .testing .assert_array_equal (got .to_numpy (), expected .to_numpy ())
281-
282- for index_got , index_expected in zip (got .index , expected .index ):
283- assert index_got == index_expected
284- else :
285- assert got == expected
286-
287-
288- @pytest .mark .parametrize (
289- 'input,expected' ,
290- [
291- (
292- {
293- "time" : datetime (
294- 1974 , 6 , 22 , 18 , 30 , 15 , tzinfo = ZoneInfo ("Etc/GMT+5" )
295- )
296- },
297- 27201.47934027778 ,
298- ),
299- (
300- {
301- "time" : datetime (1974 , 6 , 22 , 23 , 30 , 15 )
302- },
303- 27201.47934027778 ,
304- ),
305- ],
306- ids = ["datetime.datetime with tzinfo" , "datetime.datetime" ],
307- )
308- def test_datetime_to_djd (input , expected ):
309- assert tools .datetime_to_djd (input ["time" ]) == expected
310-
311-
312- @pytest .mark .parametrize (
313- 'input,expected' ,
314- [
315- (
316- {
317- "djd" : 27201.47934027778 ,
318- "tz" : "Etc/GMT+5" ,
319- },
320- datetime (1974 , 6 , 22 , 18 , 30 , 15 , tzinfo = ZoneInfo ("Etc/GMT+5" )),
321- ),
322- (
323- {
324- "djd" : 27201.47934027778 ,
325- "tz" : None ,
326- },
327- datetime (1974 , 6 , 22 , 23 , 30 , 15 , tzinfo = ZoneInfo ("UTC" )),
328- ),
329- ],
330- ids = ["djd with tzinfo" , "djd" ],
331- )
332- def test_djd_to_datetime (input , expected ):
333- if input ["tz" ] is not None :
334- got = tools .djd_to_datetime (input ["djd" ])
335- else :
336- got = tools .djd_to_datetime (input ["djd" ], tz = "Etc/GMT+5" )
337-
338- assert got == expected
152+ def test_localize_to_utc ():
153+ lat , lon = 43.2 , - 77.6
154+ tz = "Etc/GMT+5"
155+ loc = location .Location (lat , lon , tz = tz )
156+ year , month , day , hour , minute , second = 1974 , 6 , 22 , 18 , 30 , 15
157+ hour_utc = hour + 5
158+
159+ # Test all combinations of supported inputs.
160+ dt_time_aware_utc = datetime (
161+ year , month , day , hour_utc , minute , second , tzinfo = ZoneInfo ("UTC" )
162+ )
163+ dt_time_aware = datetime (
164+ year , month , day , hour , minute , second , tzinfo = ZoneInfo (tz )
165+ )
166+ assert tools .localize_to_utc (dt_time_aware , None ) == dt_time_aware_utc
167+ dt_time_naive = datetime (year , month , day , hour , minute , second )
168+ assert tools .localize_to_utc (dt_time_naive , loc ) == dt_time_aware_utc
169+
170+ # FIXME Derive timestamp strings from above variables.
171+ dt_index_aware_utc = pd .DatetimeIndex (
172+ [dt_time_aware_utc .strftime ("%Y-%m-%dT%H:%M:%S" )], tz = ZoneInfo ("UTC" )
173+ )
174+ dt_index_aware = pd .DatetimeIndex (
175+ [dt_time_aware .strftime ("%Y-%m-%dT%H:%M:%S" )], tz = ZoneInfo (tz )
176+ )
177+ assert tools .localize_to_utc (dt_index_aware , None ) == dt_index_aware_utc
178+ dt_index_naive = pd .DatetimeIndex (
179+ [dt_time_naive .strftime ("%Y-%m-%dT%H:%M:%S" )]
180+ )
181+ assert tools .localize_to_utc (dt_index_naive , loc ) == dt_index_aware_utc
182+
183+ # Older pandas versions have wonky dtype equality check on timestamp
184+ # index, so check the values as numpy.ndarray and indices one by one.
185+ series_time_aware_utc_expected = pd .Series ([24.42 ], dt_index_aware_utc )
186+ series_time_aware = pd .Series ([24.42 ], index = dt_index_aware )
187+ series_time_aware_utc_got = tools .localize_to_utc (series_time_aware , None )
188+ np .testing .assert_array_equal (
189+ series_time_aware_utc_got .to_numpy (),
190+ series_time_aware_utc_expected .to_numpy (),
191+ )
192+
193+ for index_got , index_expected in zip (
194+ series_time_aware_utc_got .index , series_time_aware_utc_expected .index
195+ ):
196+ assert index_got == index_expected
197+
198+ series_time_naive = pd .Series ([24.42 ], index = dt_index_naive )
199+ series_time_naive_utc_got = tools .localize_to_utc (series_time_naive , loc )
200+ np .testing .assert_array_equal (
201+ series_time_naive_utc_got .to_numpy (),
202+ series_time_aware_utc_expected .to_numpy (),
203+ )
204+
205+ for index_got , index_expected in zip (
206+ series_time_naive_utc_got .index , series_time_aware_utc_expected .index
207+ ):
208+ assert index_got == index_expected
209+
210+ # Older pandas versions have wonky dtype equality check on timestamp
211+ # index, so check the values as numpy.ndarray and indices one by one.
212+ df_time_aware_utc_expected = pd .DataFrame ([[24.42 ]], dt_index_aware )
213+ df_time_naive = pd .DataFrame ([[24.42 ]], index = dt_index_naive )
214+ df_time_naive_utc_got = tools .localize_to_utc (df_time_naive , loc )
215+ np .testing .assert_array_equal (
216+ df_time_naive_utc_got .to_numpy (),
217+ df_time_aware_utc_expected .to_numpy (),
218+ )
219+
220+ for index_got , index_expected in zip (
221+ df_time_naive_utc_got .index , df_time_aware_utc_expected .index
222+ ):
223+ assert index_got == index_expected
224+
225+ df_time_aware = pd .DataFrame ([[24.42 ]], index = dt_index_aware )
226+ df_time_aware_utc_got = tools .localize_to_utc (df_time_aware , None )
227+ np .testing .assert_array_equal (
228+ df_time_aware_utc_got .to_numpy (),
229+ df_time_aware_utc_expected .to_numpy (),
230+ )
231+
232+ for index_got , index_expected in zip (
233+ df_time_aware_utc_got .index , df_time_aware_utc_expected .index
234+ ):
235+ assert index_got == index_expected
236+
237+
238+ def test_datetime_to_djd ():
239+ expected = 27201.47934027778
240+ dt_aware = datetime (1974 , 6 , 22 , 18 , 30 , 15 , tzinfo = ZoneInfo ("Etc/GMT+5" ))
241+ assert tools .datetime_to_djd (dt_aware ) == expected
242+ dt_naive_utc = datetime (1974 , 6 , 22 , 23 , 30 , 15 )
243+ assert tools .datetime_to_djd (dt_naive_utc ) == expected
244+
245+
246+ def test_djd_to_datetime ():
247+ djd = 27201.47934027778
248+ tz = "Etc/GMT+5"
249+
250+ expected = datetime (1974 , 6 , 22 , 18 , 30 , 15 , tzinfo = ZoneInfo (tz ))
251+ assert tools .djd_to_datetime (djd , tz ) == expected
252+
253+ expected = datetime (1974 , 6 , 22 , 23 , 30 , 15 , tzinfo = ZoneInfo ("UTC" ))
254+ assert tools .djd_to_datetime (djd ) == expected
0 commit comments