Skip to content

Commit d20aefa

Browse files
committed
Fix formatting errors
1 parent 102339d commit d20aefa

File tree

2 files changed

+63
-44
lines changed

2 files changed

+63
-44
lines changed

pandas/io/formats/csvs.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
Sequence,
1212
)
1313
import csv as csvlib
14+
from datetime import datetime as _pydatetime
1415
import os
1516
from typing import (
1617
TYPE_CHECKING,
@@ -20,15 +21,12 @@
2021

2122
import numpy as np
2223

23-
from datetime import datetime as _pydatetime
24-
25-
from pandas.core.dtypes.dtypes import DatetimeTZDtype
26-
from pandas.core.dtypes.common import is_object_dtype
27-
2824
from pandas._libs import writers as libwriters
2925
from pandas._typing import SequenceNotStr
3026
from pandas.util._decorators import cache_readonly
3127

28+
from pandas.core.dtypes.common import is_object_dtype
29+
from pandas.core.dtypes.dtypes import DatetimeTZDtype
3230
from pandas.core.dtypes.generic import (
3331
ABCDatetimeIndex,
3432
ABCIndex,
@@ -317,7 +315,6 @@ def _save_body(self) -> None:
317315
break
318316
self._save_chunk(start_i, end_i)
319317

320-
321318
# tz-aware CSV formatting helper
322319
@staticmethod
323320
def _csv_format_datetime_tz_ea(ser, na_rep: str):
@@ -344,21 +341,24 @@ def _csv_format_py_tz_aware_obj(ser, na_rep: str):
344341

345342
def _is_tzaware_dt(x: object) -> bool:
346343
return (
347-
isinstance(x, _pydatetime)
348-
and getattr(x, "tzinfo", None) is not None
349-
and x.tzinfo.utcoffset(x) is not None
344+
isinstance(x, _pydatetime)
345+
and getattr(x, "tzinfo", None) is not None
346+
and x.tzinfo.utcoffset(x) is not None
350347
)
351348

352-
mask = np.fromiter((_is_tzaware_dt(x) for x in vals), dtype=bool, count=len(vals))
349+
mask = np.fromiter(
350+
(_is_tzaware_dt(x) for x in vals), dtype=bool, count=len(vals)
351+
)
353352
if mask.any():
354353
out = vals.copy()
355354
# isoformat gives 'YYYY-MM-DD HH:MM:SS.ffffff+HH:MM'
356-
out[mask] = [x.isoformat(sep=" ", timespec="microseconds") for x in out[mask]]
355+
out[mask] = [
356+
x.isoformat(sep=" ", timespec="microseconds") for x in out[mask]
357+
]
357358
ser = ser._constructor(out, index=ser.index, name=ser.name)
358359

359360
return ser.fillna(na_rep)
360361

361-
362362
def _save_chunk(self, start_i: int, end_i: int) -> None:
363363
# create the data for a chunk
364364
slicer = slice(start_i, end_i)

pandas/tests/io/formats/test_to_csv.py

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
from datetime import (
2+
datetime,
3+
timedelta,
4+
timezone,
5+
)
16
import io
27
import os
38
import sys
49
from zipfile import ZipFile
5-
from datetime import datetime, timezone, timedelta
610

711
from _csv import Error
812
import 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\n2025-08-14 12:34:56+0000\n2025-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

Comments
 (0)