@@ -499,6 +499,15 @@ def test_timestamp_and_label(self, version):
499
499
assert reader .time_stamp == '29 Feb 2000 14:21'
500
500
assert reader .data_label == data_label
501
501
502
+ @pytest .mark .parametrize ('version' , [114 , 117 ])
503
+ def test_invalid_timestamp (self , version ):
504
+ original = DataFrame ([(1 ,)], columns = ['variable' ])
505
+ time_stamp = '01 Jan 2000, 00:00:00'
506
+ with tm .ensure_clean () as path :
507
+ with pytest .raises (ValueError ):
508
+ original .to_stata (path , time_stamp = time_stamp ,
509
+ version = version )
510
+
502
511
def test_numeric_column_names (self ):
503
512
original = DataFrame (np .reshape (np .arange (25.0 ), (5 , 5 )))
504
513
original .index .name = 'index'
@@ -639,7 +648,8 @@ def test_write_missing_strings(self):
639
648
expected )
640
649
641
650
@pytest .mark .parametrize ('version' , [114 , 117 ])
642
- def test_bool_uint (self , version ):
651
+ @pytest .mark .parametrize ('byteorder' , ['>' , '<' ])
652
+ def test_bool_uint (self , byteorder , version ):
643
653
s0 = Series ([0 , 1 , True ], dtype = np .bool )
644
654
s1 = Series ([0 , 1 , 100 ], dtype = np .uint8 )
645
655
s2 = Series ([0 , 1 , 255 ], dtype = np .uint8 )
@@ -658,7 +668,7 @@ def test_bool_uint(self, version):
658
668
expected [c ] = expected [c ].astype (t )
659
669
660
670
with tm .ensure_clean () as path :
661
- original .to_stata (path , version = version )
671
+ original .to_stata (path , byteorder = byteorder , version = version )
662
672
written_and_read_again = self .read_dta (path )
663
673
written_and_read_again = written_and_read_again .set_index ('index' )
664
674
tm .assert_frame_equal (written_and_read_again , expected )
@@ -1173,6 +1183,29 @@ def test_write_variable_labels(self, version):
1173
1183
read_labels = sr .variable_labels ()
1174
1184
assert read_labels == variable_labels
1175
1185
1186
+ @pytest .mark .parametrize ('version' , [114 , 117 ])
1187
+ def test_invalid_variable_labels (self , version ):
1188
+ original = pd .DataFrame ({'a' : [1 , 2 , 3 , 4 ],
1189
+ 'b' : [1.0 , 3.0 , 27.0 , 81.0 ],
1190
+ 'c' : ['Atlanta' , 'Birmingham' ,
1191
+ 'Cincinnati' , 'Detroit' ]})
1192
+ original .index .name = 'index'
1193
+ variable_labels = {'a' : 'very long' * 10 ,
1194
+ 'b' : 'City Exponent' ,
1195
+ 'c' : 'City' }
1196
+ with tm .ensure_clean () as path :
1197
+ with pytest .raises (ValueError ):
1198
+ original .to_stata (path ,
1199
+ variable_labels = variable_labels ,
1200
+ version = version )
1201
+
1202
+ variable_labels ['a' ] = u'invalid character Œ'
1203
+ with tm .ensure_clean () as path :
1204
+ with pytest .raises (ValueError ):
1205
+ original .to_stata (path ,
1206
+ variable_labels = variable_labels ,
1207
+ version = version )
1208
+
1176
1209
def test_write_variable_label_errors (self ):
1177
1210
original = pd .DataFrame ({'a' : [1 , 2 , 3 , 4 ],
1178
1211
'b' : [1.0 , 3.0 , 27.0 , 81.0 ],
@@ -1220,6 +1253,13 @@ def test_default_date_conversion(self):
1220
1253
direct = read_stata (path , convert_dates = True )
1221
1254
tm .assert_frame_equal (reread , direct )
1222
1255
1256
+ dates_idx = original .columns .tolist ().index ('dates' )
1257
+ original .to_stata (path ,
1258
+ write_index = False ,
1259
+ convert_dates = {dates_idx : 'tc' })
1260
+ direct = read_stata (path , convert_dates = True )
1261
+ tm .assert_frame_equal (reread , direct )
1262
+
1223
1263
def test_unsupported_type (self ):
1224
1264
original = pd .DataFrame ({'a' : [1 + 2j , 2 + 4j ]})
1225
1265
@@ -1419,3 +1459,17 @@ def test_convert_strl_name_swap(self):
1419
1459
reread .columns = original .columns
1420
1460
tm .assert_frame_equal (reread , original ,
1421
1461
check_index_type = False )
1462
+
1463
+ def test_invalid_date_conversion (self ):
1464
+ # GH 12259
1465
+ dates = [dt .datetime (1999 , 12 , 31 , 12 , 12 , 12 , 12000 ),
1466
+ dt .datetime (2012 , 12 , 21 , 12 , 21 , 12 , 21000 ),
1467
+ dt .datetime (1776 , 7 , 4 , 7 , 4 , 7 , 4000 )]
1468
+ original = pd .DataFrame ({'nums' : [1.0 , 2.0 , 3.0 ],
1469
+ 'strs' : ['apple' , 'banana' , 'cherry' ],
1470
+ 'dates' : dates })
1471
+
1472
+ with tm .ensure_clean () as path :
1473
+ with pytest .raises (ValueError ):
1474
+ original .to_stata (path ,
1475
+ convert_dates = {'wrong_name' : 'tc' })
0 commit comments