1+ from datetime import (
2+ datetime ,
3+ timedelta ,
4+ timezone ,
5+ )
16import io
27import os
38import sys
49from zipfile import ZipFile
5- from datetime import datetime , timezone , timedelta
610
711from _csv import Error
812import numpy as np
@@ -713,37 +717,43 @@ def test_to_csv_encoding_binary_handle(self, mode):
713717 handle .seek (0 )
714718 assert handle .read ().startswith (b'\xef \xbb \xbf ""' )
715719
716-
717720 """
718721 tz-aware timestamps with/without microseconds should be written consistently
719722 Checks if the .ffffff format is consistent, even when microseconds==0
720723
721724 GH 62111
722725 """
726+
723727 def test_to_csv_tz_aware_consistent_microseconds_formatting_python_datetime (self ):
724- df = DataFrame ({"timestamp" : [
725- datetime (2025 , 8 , 14 , 12 , 34 , 56 , 0 , tzinfo = timezone .utc ),
726- datetime (2025 , 8 , 14 , 12 , 34 , 56 , 1 , tzinfo = timezone .utc ),
727- ]})
728+ df = DataFrame (
729+ {
730+ "timestamp" : [
731+ datetime (2025 , 8 , 14 , 12 , 34 , 56 , 0 , tzinfo = timezone .utc ),
732+ datetime (2025 , 8 , 14 , 12 , 34 , 56 , 1 , tzinfo = timezone .utc ),
733+ ]
734+ }
735+ )
728736 with tm .ensure_clean ("test.csv" ) as path :
729737 df .to_csv (path , index = False , lineterminator = "\n " )
730738 with open (path , encoding = "utf-8" ) as f :
731739 contents = f .read ()
732740
733- #
734741 expected = (
735742 "timestamp\n "
736743 "2025-08-14 12:34:56.000000+00:00\n "
737744 "2025-08-14 12:34:56.000001+00:00\n "
738745 )
739746 assert contents == expected
740747
741-
742748 def test_to_csv_tz_aware_consistent_microseconds_formatting_timestamp (self ):
743- df = DataFrame ({"timestamp" : [
744- pd .Timestamp ("2025-08-14 12:34:56+00:00" ),
745- pd .Timestamp ("2025-08-14 12:34:56.000001+00:00" ),
746- ]})
749+ df = DataFrame (
750+ {
751+ "timestamp" : [
752+ pd .Timestamp ("2025-08-14 12:34:56+00:00" ),
753+ pd .Timestamp ("2025-08-14 12:34:56.000001+00:00" ),
754+ ]
755+ }
756+ )
747757 with tm .ensure_clean ("test.csv" ) as path :
748758 df .to_csv (path , index = False , lineterminator = "\n " )
749759 with open (path , encoding = "utf-8" ) as f :
@@ -756,41 +766,50 @@ def test_to_csv_tz_aware_consistent_microseconds_formatting_timestamp(self):
756766 )
757767 assert contents == expected
758768
759-
760769 def test_to_csv_tz_aware_respects_date_format_python_datetime (self ):
761770 # No microseconds in date_format; %z produces +0000 (no colon) by design.
762- df = DataFrame ({"timestamp" : [
763- datetime (2025 , 8 , 14 , 12 , 34 , 56 , 0 , tzinfo = timezone .utc ),
764- datetime (2025 , 8 , 14 , 12 , 34 , 56 , 1 , tzinfo = timezone .utc ),
765- ]})
771+ df = DataFrame (
772+ {
773+ "timestamp" : [
774+ datetime (2025 , 8 , 14 , 12 , 34 , 56 , 0 , tzinfo = timezone .utc ),
775+ datetime (2025 , 8 , 14 , 12 , 34 , 56 , 1 , tzinfo = timezone .utc ),
776+ ]
777+ }
778+ )
766779 with tm .ensure_clean ("test.csv" ) as path :
767- df .to_csv (path , index = False , lineterminator = "\n " , date_format = "%Y-%m-%d %H:%M:%S%z" )
780+ df .to_csv (
781+ path ,
782+ index = False ,
783+ lineterminator = "\n " ,
784+ date_format = "%Y-%m-%d %H:%M:%S%z" ,
785+ )
768786 with open (path , encoding = "utf-8" ) as f :
769787 contents = f .read ()
770788
771- expected = (
772- "timestamp\n "
773- "2025-08-14 12:34:56+0000\n "
774- "2025-08-14 12:34:56+0000\n "
775- )
789+ expected = "timestamp\n 2025-08-14 12:34:56+0000\n 2025-08-14 12:34:56+0000\n "
776790 assert contents == expected
777791
778-
779- def test_to_csv_tz_aware_consistent_microseconds_non_utc_offset_python_datetime (self ):
780- ist = timezone (timedelta (hours = 5 , minutes = 30 )) # +05:30
781- df = DataFrame ({"timestamp" : [
782- datetime (2025 , 8 , 14 , 12 , 34 , 56 , 0 , tzinfo = ist ),
783- datetime (2025 , 8 , 14 , 12 , 34 , 56 , 1 , tzinfo = ist ),
784- ]})
792+ def test_to_csv_tz_aware_consistent_microseconds_non_utc_offset_python_datetime (
793+ self ,
794+ ):
795+ am_tz = timezone (timedelta (hours = 4 )) # +04:00 (Armenia / Asia/Yerevan)
796+ df = DataFrame (
797+ {
798+ "timestamp" : [
799+ datetime (2025 , 8 , 14 , 12 , 34 , 56 , 0 , tzinfo = am_tz ),
800+ datetime (2025 , 8 , 14 , 12 , 34 , 56 , 1 , tzinfo = am_tz ),
801+ ]
802+ }
803+ )
785804 with tm .ensure_clean ("test.csv" ) as path :
786805 df .to_csv (path , index = False , lineterminator = "\n " )
787806 with open (path , encoding = "utf-8" ) as f :
788807 contents = f .read ()
789808
790809 expected = (
791810 "timestamp\n "
792- "2025-08-14 12:34:56.000000+05:30 \n "
793- "2025-08-14 12:34:56.000001+05:30 \n "
811+ "2025-08-14 12:34:56.000000+04:00 \n "
812+ "2025-08-14 12:34:56.000001+04:00 \n "
794813 )
795814 assert contents == expected
796815
0 commit comments