1010from pandas .compat import (lmap , range , lrange , StringIO , u )
1111from pandas .parser import CParserError
1212from pandas import (DataFrame , Index , Series , MultiIndex , Timestamp ,
13- date_range , read_csv , compat )
13+ date_range , read_csv , compat , to_datetime )
1414import pandas as pd
1515
1616from pandas .util .testing import (assert_almost_equal ,
@@ -139,7 +139,7 @@ def test_to_csv_from_csv5(self):
139139 self .tzframe .to_csv (path )
140140 result = pd .read_csv (path , index_col = 0 , parse_dates = ['A' ])
141141
142- converter = lambda c : pd . to_datetime (result [c ]).dt .tz_localize (
142+ converter = lambda c : to_datetime (result [c ]).dt .tz_localize (
143143 'UTC' ).dt .tz_convert (self .tzframe [c ].dt .tz )
144144 result ['B' ] = converter ('B' )
145145 result ['C' ] = converter ('C' )
@@ -162,15 +162,6 @@ def test_to_csv_cols_reordering(self):
162162
163163 assert_frame_equal (df [cols ], rs_c , check_names = False )
164164
165- def test_to_csv_legacy_raises_on_dupe_cols (self ):
166- df = mkdf (10 , 3 )
167- df .columns = ['a' , 'a' , 'b' ]
168- with ensure_clean () as path :
169- with tm .assert_produces_warning (FutureWarning ,
170- check_stacklevel = False ):
171- self .assertRaises (NotImplementedError ,
172- df .to_csv , path , engine = 'python' )
173-
174165 def test_to_csv_new_dupe_cols (self ):
175166 import pandas as pd
176167
@@ -712,7 +703,6 @@ def test_to_csv_dups_cols(self):
712703 cols .extend ([0 , 1 , 2 ])
713704 df .columns = cols
714705
715- from pandas import to_datetime
716706 with ensure_clean () as filename :
717707 df .to_csv (filename )
718708 result = read_csv (filename , index_col = 0 )
@@ -993,72 +983,57 @@ def test_to_csv_compression_value_error(self):
993983 filename , compression = "zip" )
994984
995985 def test_to_csv_date_format (self ):
996- from pandas import to_datetime
997986 with ensure_clean ('__tmp_to_csv_date_format__' ) as path :
998- for engine in [None , 'python' ]:
999- w = FutureWarning if engine == 'python' else None
1000-
1001- dt_index = self .tsframe .index
1002- datetime_frame = DataFrame (
1003- {'A' : dt_index , 'B' : dt_index .shift (1 )}, index = dt_index )
1004-
1005- with tm .assert_produces_warning (w , check_stacklevel = False ):
1006- datetime_frame .to_csv (
1007- path , date_format = '%Y%m%d' , engine = engine )
1008-
1009- # Check that the data was put in the specified format
1010- test = read_csv (path , index_col = 0 )
1011-
1012- datetime_frame_int = datetime_frame .applymap (
1013- lambda x : int (x .strftime ('%Y%m%d' )))
1014- datetime_frame_int .index = datetime_frame_int .index .map (
1015- lambda x : int (x .strftime ('%Y%m%d' )))
987+ dt_index = self .tsframe .index
988+ datetime_frame = DataFrame (
989+ {'A' : dt_index , 'B' : dt_index .shift (1 )}, index = dt_index )
990+ datetime_frame .to_csv (path , date_format = '%Y%m%d' )
1016991
1017- assert_frame_equal (test , datetime_frame_int )
992+ # Check that the data was put in the specified format
993+ test = read_csv (path , index_col = 0 )
1018994
1019- with tm .assert_produces_warning (w , check_stacklevel = False ):
1020- datetime_frame .to_csv (
1021- path , date_format = '%Y-%m-%d' , engine = engine )
995+ datetime_frame_int = datetime_frame .applymap (
996+ lambda x : int (x .strftime ('%Y%m%d' )))
997+ datetime_frame_int .index = datetime_frame_int .index .map (
998+ lambda x : int (x .strftime ('%Y%m%d' )))
1022999
1023- # Check that the data was put in the specified format
1024- test = read_csv (path , index_col = 0 )
1025- datetime_frame_str = datetime_frame .applymap (
1026- lambda x : x .strftime ('%Y-%m-%d' ))
1027- datetime_frame_str .index = datetime_frame_str .index .map (
1028- lambda x : x .strftime ('%Y-%m-%d' ))
1000+ assert_frame_equal (test , datetime_frame_int )
10291001
1030- assert_frame_equal ( test , datetime_frame_str )
1002+ datetime_frame . to_csv ( path , date_format = '%Y-%m-%d' )
10311003
1032- # Check that columns get converted
1033- datetime_frame_columns = datetime_frame .T
1004+ # Check that the data was put in the specified format
1005+ test = read_csv (path , index_col = 0 )
1006+ datetime_frame_str = datetime_frame .applymap (
1007+ lambda x : x .strftime ('%Y-%m-%d' ))
1008+ datetime_frame_str .index = datetime_frame_str .index .map (
1009+ lambda x : x .strftime ('%Y-%m-%d' ))
10341010
1035- with tm .assert_produces_warning (w , check_stacklevel = False ):
1036- datetime_frame_columns .to_csv (
1037- path , date_format = '%Y%m%d' , engine = engine )
1011+ assert_frame_equal (test , datetime_frame_str )
10381012
1039- test = read_csv (path , index_col = 0 )
1013+ # Check that columns get converted
1014+ datetime_frame_columns = datetime_frame .T
1015+ datetime_frame_columns .to_csv (path , date_format = '%Y%m%d' )
10401016
1041- datetime_frame_columns = datetime_frame_columns .applymap (
1042- lambda x : int (x .strftime ('%Y%m%d' )))
1043- # Columns don't get converted to ints by read_csv
1044- datetime_frame_columns .columns = (
1045- datetime_frame_columns .columns
1046- .map (lambda x : x .strftime ('%Y%m%d' )))
1017+ test = read_csv (path , index_col = 0 )
10471018
1048- assert_frame_equal (test , datetime_frame_columns )
1019+ datetime_frame_columns = datetime_frame_columns .applymap (
1020+ lambda x : int (x .strftime ('%Y%m%d' )))
1021+ # Columns don't get converted to ints by read_csv
1022+ datetime_frame_columns .columns = (
1023+ datetime_frame_columns .columns
1024+ .map (lambda x : x .strftime ('%Y%m%d' )))
10491025
1050- # test NaTs
1051- nat_index = to_datetime (
1052- ['NaT' ] * 10 + ['2000-01-01' , '1/1/2000' , '1-1-2000' ])
1053- nat_frame = DataFrame ({'A' : nat_index }, index = nat_index )
1026+ assert_frame_equal (test , datetime_frame_columns )
10541027
1055- with tm .assert_produces_warning (w , check_stacklevel = False ):
1056- nat_frame .to_csv (
1057- path , date_format = '%Y-%m-%d' , engine = engine )
1028+ # test NaTs
1029+ nat_index = to_datetime (
1030+ ['NaT' ] * 10 + ['2000-01-01' , '1/1/2000' , '1-1-2000' ])
1031+ nat_frame = DataFrame ({'A' : nat_index }, index = nat_index )
1032+ nat_frame .to_csv (path , date_format = '%Y-%m-%d' )
10581033
1059- test = read_csv (path , parse_dates = [0 , 1 ], index_col = 0 )
1034+ test = read_csv (path , parse_dates = [0 , 1 ], index_col = 0 )
10601035
1061- assert_frame_equal (test , nat_frame )
1036+ assert_frame_equal (test , nat_frame )
10621037
10631038 def test_to_csv_with_dst_transitions (self ):
10641039
@@ -1077,7 +1052,7 @@ def test_to_csv_with_dst_transitions(self):
10771052 # we have to reconvert the index as we
10781053 # don't parse the tz's
10791054 result = read_csv (path , index_col = 0 )
1080- result .index = pd . to_datetime (result .index ).tz_localize (
1055+ result .index = to_datetime (result .index ).tz_localize (
10811056 'UTC' ).tz_convert ('Europe/London' )
10821057 assert_frame_equal (result , df )
10831058
@@ -1089,9 +1064,9 @@ def test_to_csv_with_dst_transitions(self):
10891064 with ensure_clean ('csv_date_format_with_dst' ) as path :
10901065 df .to_csv (path , index = True )
10911066 result = read_csv (path , index_col = 0 )
1092- result .index = pd . to_datetime (result .index ).tz_localize (
1067+ result .index = to_datetime (result .index ).tz_localize (
10931068 'UTC' ).tz_convert ('Europe/Paris' )
1094- result ['idx' ] = pd . to_datetime (result ['idx' ]).astype (
1069+ result ['idx' ] = to_datetime (result ['idx' ]).astype (
10951070 'datetime64[ns, Europe/Paris]' )
10961071 assert_frame_equal (result , df )
10971072
0 commit comments