8
8
import pandas as pd
9
9
from pandas import (
10
10
DatetimeIndex ,
11
+ NaT ,
11
12
Series ,
12
13
)
13
14
import pandas ._testing as tm
@@ -33,7 +34,7 @@ def test_get_values_for_csv():
33
34
tm .assert_numpy_array_equal (result , expected )
34
35
35
36
# NULL object handling should work
36
- index = DatetimeIndex (["2017-01-01" , pd . NaT , "2017-01-03" ])
37
+ index = DatetimeIndex (["2017-01-01" , NaT , "2017-01-03" ])
37
38
expected = np .array (["2017-01-01" , "NaT" , "2017-01-03" ], dtype = object )
38
39
39
40
result = index ._get_values_for_csv (na_rep = "NaT" )
@@ -58,6 +59,20 @@ def test_get_values_for_csv():
58
59
59
60
60
61
class TestDatetimeIndexRendering :
62
+ def test_dti_repr_dates (self ):
63
+ text = str (pd .to_datetime ([datetime (2013 , 1 , 1 ), datetime (2014 , 1 , 1 )]))
64
+ assert "['2013-01-01'," in text
65
+ assert ", '2014-01-01']" in text
66
+
67
+ def test_dti_repr_mixed (self ):
68
+ text = str (
69
+ pd .to_datetime (
70
+ [datetime (2013 , 1 , 1 ), datetime (2014 , 1 , 1 , 12 ), datetime (2014 , 1 , 1 )]
71
+ )
72
+ )
73
+ assert "'2013-01-01 00:00:00'," in text
74
+ assert "'2014-01-01 00:00:00']" in text
75
+
61
76
def test_dti_repr_short (self ):
62
77
dr = pd .date_range (start = "1/1/2012" , periods = 1 )
63
78
repr (dr )
@@ -114,11 +129,11 @@ def test_dti_representation(self, method):
114
129
)
115
130
idxs .append (
116
131
DatetimeIndex (
117
- ["2011-01-01 09:00" , "2011-01-01 10:00" , pd . NaT ], tz = "US/Eastern"
132
+ ["2011-01-01 09:00" , "2011-01-01 10:00" , NaT ], tz = "US/Eastern"
118
133
)
119
134
)
120
135
idxs .append (
121
- DatetimeIndex (["2011-01-01 09:00" , "2011-01-01 10:00" , pd . NaT ], tz = "UTC" )
136
+ DatetimeIndex (["2011-01-01 09:00" , "2011-01-01 10:00" , NaT ], tz = "UTC" )
122
137
)
123
138
124
139
exp = []
@@ -165,7 +180,7 @@ def test_dti_representation_to_series(self):
165
180
tz = "Asia/Tokyo" ,
166
181
)
167
182
idx6 = DatetimeIndex (
168
- ["2011-01-01 09:00" , "2011-01-01 10:00" , pd . NaT ], tz = "US/Eastern"
183
+ ["2011-01-01 09:00" , "2011-01-01 10:00" , NaT ], tz = "US/Eastern"
169
184
)
170
185
idx7 = DatetimeIndex (["2011-01-01 09:00" , "2011-01-02 10:15" ])
171
186
@@ -222,7 +237,7 @@ def test_dti_summary(self):
222
237
tz = "Asia/Tokyo" ,
223
238
)
224
239
idx6 = DatetimeIndex (
225
- ["2011-01-01 09:00" , "2011-01-01 10:00" , pd . NaT ], tz = "US/Eastern"
240
+ ["2011-01-01 09:00" , "2011-01-01 10:00" , NaT ], tz = "US/Eastern"
226
241
)
227
242
228
243
exp1 = "DatetimeIndex: 0 entries\n Freq: D"
@@ -281,6 +296,14 @@ def test_dti_custom_business_summary_dateutil(self):
281
296
282
297
283
298
class TestFormat :
299
+ def test_format (self ):
300
+ # GH#35439
301
+ idx = pd .date_range ("20130101" , periods = 5 )
302
+ expected = [f"{ x :%Y-%m-%d} " for x in idx ]
303
+ msg = r"DatetimeIndex\.format is deprecated"
304
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
305
+ assert idx .format () == expected
306
+
284
307
def test_format_with_name_time_info (self ):
285
308
# bug I fixed 12/20/2011
286
309
dates = pd .date_range ("2011-01-01 04:00:00" , periods = 10 , name = "something" )
@@ -299,3 +322,37 @@ def test_format_datetime_with_time(self):
299
322
expected = ["2012-02-07 00:00:00" , "2012-02-07 23:00:00" ]
300
323
assert len (result ) == 2
301
324
assert result == expected
325
+
326
+ def test_format_datetime (self ):
327
+ msg = "DatetimeIndex.format is deprecated"
328
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
329
+ formatted = pd .to_datetime ([datetime (2003 , 1 , 1 , 12 ), NaT ]).format ()
330
+ assert formatted [0 ] == "2003-01-01 12:00:00"
331
+ assert formatted [1 ] == "NaT"
332
+
333
+ def test_format_date (self ):
334
+ msg = "DatetimeIndex.format is deprecated"
335
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
336
+ formatted = pd .to_datetime ([datetime (2003 , 1 , 1 ), NaT ]).format ()
337
+ assert formatted [0 ] == "2003-01-01"
338
+ assert formatted [1 ] == "NaT"
339
+
340
+ def test_format_date_tz (self ):
341
+ dti = pd .to_datetime ([datetime (2013 , 1 , 1 )], utc = True )
342
+ msg = "DatetimeIndex.format is deprecated"
343
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
344
+ formatted = dti .format ()
345
+ assert formatted [0 ] == "2013-01-01 00:00:00+00:00"
346
+
347
+ dti = pd .to_datetime ([datetime (2013 , 1 , 1 ), NaT ], utc = True )
348
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
349
+ formatted = dti .format ()
350
+ assert formatted [0 ] == "2013-01-01 00:00:00+00:00"
351
+
352
+ def test_format_date_explicit_date_format (self ):
353
+ dti = pd .to_datetime ([datetime (2003 , 2 , 1 ), NaT ])
354
+ msg = "DatetimeIndex.format is deprecated"
355
+ with tm .assert_produces_warning (FutureWarning , match = msg ):
356
+ formatted = dti .format (date_format = "%m-%d-%Y" , na_rep = "UT" )
357
+ assert formatted [0 ] == "02-01-2003"
358
+ assert formatted [1 ] == "UT"
0 commit comments