Skip to content

Commit 102339d

Browse files
committed
TST: add coverage for tz-aware CSV formatting consistency (GH#62111)
1 parent 7b0baa9 commit 102339d

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

pandas/tests/io/formats/test_to_csv.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
import sys
44
from zipfile import ZipFile
5-
from datetime import datetime, timezone
5+
from datetime import datetime, timezone, timedelta
66

77
from _csv import Error
88
import numpy as np
@@ -738,6 +738,63 @@ def test_to_csv_tz_aware_consistent_microseconds_formatting_python_datetime(self
738738
)
739739
assert contents == expected
740740

741+
742+
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+
]})
747+
with tm.ensure_clean("test.csv") as path:
748+
df.to_csv(path, index=False, lineterminator="\n")
749+
with open(path, encoding="utf-8") as f:
750+
contents = f.read()
751+
752+
expected = (
753+
"timestamp\n"
754+
"2025-08-14 12:34:56.000000+00:00\n"
755+
"2025-08-14 12:34:56.000001+00:00\n"
756+
)
757+
assert contents == expected
758+
759+
760+
def test_to_csv_tz_aware_respects_date_format_python_datetime(self):
761+
# 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+
]})
766+
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")
768+
with open(path, encoding="utf-8") as f:
769+
contents = f.read()
770+
771+
expected = (
772+
"timestamp\n"
773+
"2025-08-14 12:34:56+0000\n"
774+
"2025-08-14 12:34:56+0000\n"
775+
)
776+
assert contents == expected
777+
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+
]})
785+
with tm.ensure_clean("test.csv") as path:
786+
df.to_csv(path, index=False, lineterminator="\n")
787+
with open(path, encoding="utf-8") as f:
788+
contents = f.read()
789+
790+
expected = (
791+
"timestamp\n"
792+
"2025-08-14 12:34:56.000000+05:30\n"
793+
"2025-08-14 12:34:56.000001+05:30\n"
794+
)
795+
assert contents == expected
796+
797+
741798
def test_to_csv_iterative_compression_name(compression):
742799
# GH 38714
743800
df = DataFrame(

0 commit comments

Comments
 (0)