@@ -2593,24 +2593,32 @@ def test_many_strl(temp_file, version):
2593
2593
def test_convert_dates_key_handling (tmp_path , version ):
2594
2594
temp_file = tmp_path / "test.dta"
2595
2595
df = DataFrame ({"old_name" : [1 , 2 , 3 ], "some_other_name" : [4 , 5 , 6 ]})
2596
- writer = StataWriter (temp_file , df )
2597
2596
2598
2597
# 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" }
2598
+ convert_dates = {"old_name" : "converted_date" }
2599
+ df .rename (columns = {"old_name" : "new_name" }, inplace = True )
2600
+ with StataWriter (
2601
+ temp_file ,
2602
+ df ,
2603
+ convert_dates = convert_dates ,
2604
+ version = version ,
2605
+ ) as writer :
2606
+ writer .write_file ()
2607
+ result = read_stata (temp_file )
2608
+ assert list (result .columns ) == ["new_name" , "some_other_name" ]
2609
+ assert "converted_date" in result .columns
2607
2610
2608
2611
# 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" }
2612
+ df = DataFrame ({"old_name" : [1 , 2 , 3 ], "some_other_name" : [4 , 5 , 6 ]})
2613
+ convert_dates = {"some_other_name" : "converted_date" }
2614
+ df .rename (columns = {"old_name" : "new_name" }, inplace = True )
2615
+ with StataWriter (
2616
+ temp_file ,
2617
+ df ,
2618
+ convert_dates = convert_dates ,
2619
+ version = version ,
2620
+ ) as writer :
2621
+ writer .write_file ()
2622
+ result = read_stata (temp_file )
2623
+ assert list (result .columns ) == ["new_name" , "some_other_name" ]
2624
+ assert "converted_date" in result .columns
0 commit comments