10
10
from pandas .compat import (lmap , range , lrange , StringIO , u )
11
11
from pandas .parser import CParserError
12
12
from pandas import (DataFrame , Index , Series , MultiIndex , Timestamp ,
13
- date_range , read_csv , compat )
13
+ date_range , read_csv , compat , to_datetime )
14
14
import pandas as pd
15
15
16
16
from pandas .util .testing import (assert_almost_equal ,
@@ -139,7 +139,7 @@ def test_to_csv_from_csv5(self):
139
139
self .tzframe .to_csv (path )
140
140
result = pd .read_csv (path , index_col = 0 , parse_dates = ['A' ])
141
141
142
- converter = lambda c : pd . to_datetime (result [c ]).dt .tz_localize (
142
+ converter = lambda c : to_datetime (result [c ]).dt .tz_localize (
143
143
'UTC' ).dt .tz_convert (self .tzframe [c ].dt .tz )
144
144
result ['B' ] = converter ('B' )
145
145
result ['C' ] = converter ('C' )
@@ -162,15 +162,6 @@ def test_to_csv_cols_reordering(self):
162
162
163
163
assert_frame_equal (df [cols ], rs_c , check_names = False )
164
164
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
-
174
165
def test_to_csv_new_dupe_cols (self ):
175
166
import pandas as pd
176
167
@@ -712,7 +703,6 @@ def test_to_csv_dups_cols(self):
712
703
cols .extend ([0 , 1 , 2 ])
713
704
df .columns = cols
714
705
715
- from pandas import to_datetime
716
706
with ensure_clean () as filename :
717
707
df .to_csv (filename )
718
708
result = read_csv (filename , index_col = 0 )
@@ -993,72 +983,57 @@ def test_to_csv_compression_value_error(self):
993
983
filename , compression = "zip" )
994
984
995
985
def test_to_csv_date_format (self ):
996
- from pandas import to_datetime
997
986
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' )
1016
991
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 )
1018
994
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' )))
1022
999
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 )
1029
1001
1030
- assert_frame_equal ( test , datetime_frame_str )
1002
+ datetime_frame . to_csv ( path , date_format = '%Y-%m-%d' )
1031
1003
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' ))
1034
1010
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 )
1038
1012
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' )
1040
1016
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 )
1047
1018
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' )))
1049
1025
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 )
1054
1027
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' )
1058
1033
1059
- test = read_csv (path , parse_dates = [0 , 1 ], index_col = 0 )
1034
+ test = read_csv (path , parse_dates = [0 , 1 ], index_col = 0 )
1060
1035
1061
- assert_frame_equal (test , nat_frame )
1036
+ assert_frame_equal (test , nat_frame )
1062
1037
1063
1038
def test_to_csv_with_dst_transitions (self ):
1064
1039
@@ -1077,7 +1052,7 @@ def test_to_csv_with_dst_transitions(self):
1077
1052
# we have to reconvert the index as we
1078
1053
# don't parse the tz's
1079
1054
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 (
1081
1056
'UTC' ).tz_convert ('Europe/London' )
1082
1057
assert_frame_equal (result , df )
1083
1058
@@ -1089,9 +1064,9 @@ def test_to_csv_with_dst_transitions(self):
1089
1064
with ensure_clean ('csv_date_format_with_dst' ) as path :
1090
1065
df .to_csv (path , index = True )
1091
1066
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 (
1093
1068
'UTC' ).tz_convert ('Europe/Paris' )
1094
- result ['idx' ] = pd . to_datetime (result ['idx' ]).astype (
1069
+ result ['idx' ] = to_datetime (result ['idx' ]).astype (
1095
1070
'datetime64[ns, Europe/Paris]' )
1096
1071
assert_frame_equal (result , df )
1097
1072
0 commit comments