@@ -45,6 +45,32 @@ def test_ops_properties_basic(self):
45
45
self .assertEqual (s .day ,10 )
46
46
self .assertRaises (AttributeError , lambda : s .weekday )
47
47
48
+ def test_astype_str (self ):
49
+ # test astype string - #10442
50
+ result = date_range ('2012-01-01' , periods = 4 , name = 'test_name' ).astype (str )
51
+ expected = Index (['2012-01-01' , '2012-01-02' , '2012-01-03' ,'2012-01-04' ],
52
+ name = 'test_name' , dtype = object )
53
+ tm .assert_index_equal (result , expected )
54
+
55
+ # test astype string with tz and name
56
+ result = date_range ('2012-01-01' , periods = 3 , name = 'test_name' , tz = 'US/Eastern' ).astype (str )
57
+ expected = Index (['2012-01-01 00:00:00-05:00' , '2012-01-02 00:00:00-05:00' ,
58
+ '2012-01-03 00:00:00-05:00' ], name = 'test_name' , dtype = object )
59
+ tm .assert_index_equal (result , expected )
60
+
61
+ # test astype string with freqH and name
62
+ result = date_range ('1/1/2011' , periods = 3 , freq = 'H' , name = 'test_name' ).astype (str )
63
+ expected = Index (['2011-01-01 00:00:00' , '2011-01-01 01:00:00' , '2011-01-01 02:00:00' ],
64
+ name = 'test_name' , dtype = object )
65
+ tm .assert_index_equal (result , expected )
66
+
67
+ # test astype string with freqH and timezone
68
+ result = date_range ('3/6/2012 00:00' , periods = 2 , freq = 'H' ,
69
+ tz = 'Europe/London' , name = 'test_name' ).astype (str )
70
+ expected = Index (['2012-03-06 00:00:00+00:00' , '2012-03-06 01:00:00+00:00' ],
71
+ dtype = object , name = 'test_name' )
72
+ tm .assert_index_equal (result , expected )
73
+
48
74
def test_asobject_tolist (self ):
49
75
idx = pd .date_range (start = '2013-01-01' , periods = 4 , freq = 'M' , name = 'idx' )
50
76
expected_list = [pd .Timestamp ('2013-01-31' ), pd .Timestamp ('2013-02-28' ),
@@ -503,7 +529,6 @@ def test_infer_freq(self):
503
529
tm .assert_index_equal (idx , result )
504
530
self .assertEqual (result .freq , freq )
505
531
506
-
507
532
class TestTimedeltaIndexOps (Ops ):
508
533
509
534
def setUp (self ):
0 commit comments