|
2 | 2 | import os |
3 | 3 | import sys |
4 | 4 | from zipfile import ZipFile |
5 | | -from datetime import datetime, timezone |
| 5 | +from datetime import datetime, timezone, timedelta |
6 | 6 |
|
7 | 7 | from _csv import Error |
8 | 8 | import numpy as np |
@@ -738,6 +738,63 @@ def test_to_csv_tz_aware_consistent_microseconds_formatting_python_datetime(self |
738 | 738 | ) |
739 | 739 | assert contents == expected |
740 | 740 |
|
| 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 | + |
741 | 798 | def test_to_csv_iterative_compression_name(compression): |
742 | 799 | # GH 38714 |
743 | 800 | df = DataFrame( |
|
0 commit comments