@@ -2587,3 +2587,30 @@ def test_many_strl(temp_file, version):
2587
2587
lbls = ["" .join (v ) for v in itertools .product (* ([string .ascii_letters ] * 3 ))]
2588
2588
value_labels = {"col" : {i : lbls [i ] for i in range (n )}}
2589
2589
df .to_stata (temp_file , value_labels = value_labels , version = version )
2590
+
2591
+
2592
+ @pytest .mark .parametrize ("version" , [114 , 117 , 118 , 119 , None ])
2593
+ def test_convert_dates_key_handling (tmp_path , version ):
2594
+ temp_file = tmp_path / "test.dta"
2595
+ df = DataFrame ({"old_name" : [1 , 2 , 3 ], "some_other_name" : [4 , 5 , 6 ]})
2596
+ writer = StataWriter (temp_file , df )
2597
+
2598
+ # Case 1: Key exists in _convert_dates
2599
+ writer ._convert_dates = {"old_name" : "converted_date" }
2600
+ columns = ["new_name" ]
2601
+ original_columns = ["old_name" ]
2602
+ for c , o in zip (columns , original_columns ):
2603
+ if c != o and o in writer ._convert_dates :
2604
+ writer ._convert_dates [c ] = writer ._convert_dates [o ]
2605
+ del writer ._convert_dates [o ]
2606
+ assert writer ._convert_dates == {"new_name" : "converted_date" }
2607
+
2608
+ # Case 2: Key does not exist in _convert_dates
2609
+ writer ._convert_dates = {"some_other_name" : "converted_date" }
2610
+ columns = ["new_name" ]
2611
+ original_columns = ["old_name" ]
2612
+ for c , o in zip (columns , original_columns ):
2613
+ if c != o and o in writer ._convert_dates :
2614
+ writer ._convert_dates [c ] = writer ._convert_dates [o ]
2615
+ del writer ._convert_dates [o ]
2616
+ assert writer ._convert_dates == {"some_other_name" : "converted_date" }
0 commit comments