-
-
Notifications
You must be signed in to change notification settings - Fork 19.3k
BUG: Fix to_csv microsecond inconsistency (#62111) #62139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
prazian
wants to merge
6
commits into
pandas-dev:main
from
prazian:bug-62111/to_csv-microsecond-inconsistency
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
85d39a2
Add test
prazian 7b0baa9
Fixes inconsistent format
prazian 102339d
TST: add coverage for tz-aware CSV formatting consistency (GH#62111)
prazian d20aefa
Fix formatting errors
prazian 794ed5a
Add missing types
prazian a5fa547
Add an entry in the latest whatsnew rst doc file
prazian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
| Sequence, | ||
| ) | ||
| import csv as csvlib | ||
| from datetime import datetime as _pydatetime | ||
| import os | ||
| from typing import ( | ||
| TYPE_CHECKING, | ||
|
|
@@ -24,6 +25,8 @@ | |
| from pandas._typing import SequenceNotStr | ||
| from pandas.util._decorators import cache_readonly | ||
|
|
||
| from pandas.core.dtypes.common import is_object_dtype | ||
| from pandas.core.dtypes.dtypes import DatetimeTZDtype | ||
| from pandas.core.dtypes.generic import ( | ||
| ABCDatetimeIndex, | ||
| ABCIndex, | ||
|
|
@@ -47,6 +50,8 @@ | |
| npt, | ||
| ) | ||
|
|
||
| from pandas.core.series import Series | ||
|
|
||
| from pandas.io.formats.format import DataFrameFormatter | ||
|
|
||
|
|
||
|
|
@@ -312,11 +317,64 @@ def _save_body(self) -> None: | |
| break | ||
| self._save_chunk(start_i, end_i) | ||
|
|
||
| # tz-aware CSV formatting helper | ||
| @staticmethod | ||
| def _csv_format_datetime_tz_ea(ser: Series, na_rep: str) -> Series: | ||
| """ | ||
| Consistent tz-aware formatting for ExtensionArray datetimes: | ||
| 'YYYY-MM-DD HH:MM:SS.ffffff+HH:MM' | ||
| """ | ||
| # +HHMM → +HH:MM | ||
| s = ser.dt.strftime("%Y-%m-%d %H:%M:%S.%f%z") | ||
| s = s.str.replace(r"([+-]\d{2})(\d{2})$", r"\1:\2", regex=True) | ||
| return s.fillna(na_rep) | ||
|
|
||
| # tz-aware CSV formatting helper | ||
| @staticmethod | ||
| def _csv_format_py_tz_aware_obj(ser: Series, na_rep: str) -> Series: | ||
| """ | ||
| For object-dtype Series containing stdlib tz-aware datetimes, render | ||
| with microseconds and colonized offset. Leave other objects untouched. | ||
| """ | ||
| if ser.empty: | ||
| return ser.astype(str) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why astype(str)? |
||
|
|
||
| vals = ser.to_numpy(object, copy=False) | ||
|
|
||
| def _is_tzaware_dt(x: object) -> bool: | ||
| if not isinstance(x, _pydatetime): | ||
| return False | ||
| tz = getattr(x, "tzinfo", None) | ||
| return tz is not None and tz.utcoffset(x) is not None | ||
|
|
||
| mask = np.fromiter( | ||
| (_is_tzaware_dt(x) for x in vals), dtype=bool, count=len(vals) | ||
| ) | ||
| if mask.any(): | ||
| out = vals.copy() | ||
| # isoformat gives 'YYYY-MM-DD HH:MM:SS.ffffff+HH:MM' | ||
| out[mask] = [ | ||
| x.isoformat(sep=" ", timespec="microseconds") for x in out[mask] | ||
| ] | ||
| ser = ser._constructor(out, index=ser.index, name=ser.name) | ||
|
|
||
| return ser.fillna(na_rep) | ||
|
|
||
| def _save_chunk(self, start_i: int, end_i: int) -> None: | ||
| # create the data for a chunk | ||
| slicer = slice(start_i, end_i) | ||
| df = self.obj.iloc[slicer] | ||
|
|
||
| # If user didn't set date_format, normalize tz-aware datetimes to a | ||
| # single canonical string form for CSV (GH 62111). | ||
| if self.date_format is None: | ||
| for col in df.columns: | ||
| col_dtype = df.dtypes[col] | ||
| if isinstance(col_dtype, DatetimeTZDtype): | ||
| df[col] = self._csv_format_datetime_tz_ea(df[col], self.na_rep) | ||
| elif is_object_dtype(col_dtype): | ||
| df[col] = self._csv_format_py_tz_aware_obj(df[col], self.na_rep) | ||
|
|
||
| res = df._get_values_for_csv(**self._number_format) | ||
| data = list(res._iter_column_arrays()) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,8 @@ | ||
| from datetime import ( | ||
| datetime, | ||
| timedelta, | ||
| timezone, | ||
| ) | ||
| import io | ||
| import os | ||
| import sys | ||
|
|
@@ -712,6 +717,102 @@ def test_to_csv_encoding_binary_handle(self, mode): | |
| handle.seek(0) | ||
| assert handle.read().startswith(b'\xef\xbb\xbf""') | ||
|
|
||
| """ | ||
| tz-aware timestamps with/without microseconds should be written consistently | ||
| Checks if the .ffffff format is consistent, even when microseconds==0 | ||
|
|
||
| GH 62111 | ||
| """ | ||
|
Comment on lines
+720
to
+725
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you move this inside the test function. |
||
|
|
||
| def test_to_csv_tz_aware_consistent_microseconds_formatting_python_datetime(self): | ||
| df = DataFrame( | ||
| { | ||
| "timestamp": [ | ||
| datetime(2025, 8, 14, 12, 34, 56, 0, tzinfo=timezone.utc), | ||
| datetime(2025, 8, 14, 12, 34, 56, 1, tzinfo=timezone.utc), | ||
| ] | ||
| } | ||
| ) | ||
| with tm.ensure_clean("test.csv") as path: | ||
| df.to_csv(path, index=False, lineterminator="\n") | ||
| with open(path, encoding="utf-8") as f: | ||
| contents = f.read() | ||
|
|
||
| expected = ( | ||
| "timestamp\n" | ||
| "2025-08-14 12:34:56.000000+00:00\n" | ||
| "2025-08-14 12:34:56.000001+00:00\n" | ||
| ) | ||
| assert contents == expected | ||
|
|
||
| def test_to_csv_tz_aware_consistent_microseconds_formatting_timestamp(self): | ||
| df = DataFrame( | ||
| { | ||
| "timestamp": [ | ||
| pd.Timestamp("2025-08-14 12:34:56+00:00"), | ||
| pd.Timestamp("2025-08-14 12:34:56.000001+00:00"), | ||
| ] | ||
| } | ||
| ) | ||
| with tm.ensure_clean("test.csv") as path: | ||
| df.to_csv(path, index=False, lineterminator="\n") | ||
| with open(path, encoding="utf-8") as f: | ||
| contents = f.read() | ||
|
|
||
| expected = ( | ||
| "timestamp\n" | ||
| "2025-08-14 12:34:56.000000+00:00\n" | ||
| "2025-08-14 12:34:56.000001+00:00\n" | ||
| ) | ||
| assert contents == expected | ||
|
|
||
| def test_to_csv_tz_aware_respects_date_format_python_datetime(self): | ||
| # No microseconds in date_format; %z produces +0000 (no colon) by design. | ||
| df = DataFrame( | ||
| { | ||
| "timestamp": [ | ||
| datetime(2025, 8, 14, 12, 34, 56, 0, tzinfo=timezone.utc), | ||
| datetime(2025, 8, 14, 12, 34, 56, 1, tzinfo=timezone.utc), | ||
| ] | ||
| } | ||
| ) | ||
| with tm.ensure_clean("test.csv") as path: | ||
| df.to_csv( | ||
| path, | ||
| index=False, | ||
| lineterminator="\n", | ||
| date_format="%Y-%m-%d %H:%M:%S%z", | ||
| ) | ||
| with open(path, encoding="utf-8") as f: | ||
| contents = f.read() | ||
|
|
||
| expected = "timestamp\n2025-08-14 12:34:56+0000\n2025-08-14 12:34:56+0000\n" | ||
| assert contents == expected | ||
|
|
||
| def test_to_csv_tz_aware_consistent_microseconds_non_utc_offset_python_datetime( | ||
| self, | ||
| ): | ||
| am_tz = timezone(timedelta(hours=4)) # +04:00 (Armenia / Asia/Yerevan) | ||
| df = DataFrame( | ||
| { | ||
| "timestamp": [ | ||
| datetime(2025, 8, 14, 12, 34, 56, 0, tzinfo=am_tz), | ||
| datetime(2025, 8, 14, 12, 34, 56, 1, tzinfo=am_tz), | ||
| ] | ||
| } | ||
| ) | ||
| with tm.ensure_clean("test.csv") as path: | ||
| df.to_csv(path, index=False, lineterminator="\n") | ||
| with open(path, encoding="utf-8") as f: | ||
| contents = f.read() | ||
|
|
||
| expected = ( | ||
| "timestamp\n" | ||
| "2025-08-14 12:34:56.000000+04:00\n" | ||
| "2025-08-14 12:34:56.000001+04:00\n" | ||
| ) | ||
| assert contents == expected | ||
|
|
||
|
|
||
| def test_to_csv_iterative_compression_name(compression): | ||
| # GH 38714 | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not clear to me why this replace is necessary, can you give a case?