Skip to content

BUG: Fix formatting of complex floats with exponents #60405

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
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,11 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
-i "pandas.core.resample.Resampler.std SA01" \
-i "pandas.core.resample.Resampler.transform PR01,RT03,SA01" \
-i "pandas.core.resample.Resampler.var SA01" \
-i "pandas.errors.DuplicateLabelError SA01" \
-i "pandas.errors.IntCastingNaNError SA01" \
-i "pandas.errors.InvalidIndexError SA01" \
-i "pandas.errors.NullFrequencyError SA01" \
-i "pandas.errors.NumExprClobberingError SA01" \
-i "pandas.errors.NumbaUtilError SA01" \
-i "pandas.errors.OutOfBoundsTimedelta SA01" \
-i "pandas.errors.PerformanceWarning SA01" \
-i "pandas.errors.PossibleDataLossError SA01" \
-i "pandas.errors.UndefinedVariableError PR01,SA01" \
-i "pandas.errors.UnsortedIndexError SA01" \
-i "pandas.errors.ValueLabelTypeMismatch SA01" \
-i "pandas.infer_freq SA01" \
-i "pandas.io.json.build_table_schema PR07,RT03,SA01" \
Expand Down
6 changes: 3 additions & 3 deletions doc/source/user_guide/window.rst
Original file line number Diff line number Diff line change
Expand Up @@ -567,9 +567,9 @@ One must have :math:`0 < \alpha \leq 1`, and while it is possible to pass

\alpha =
\begin{cases}
\frac{2}{s + 1}, & \text{for span}\ s \geq 1\\
\frac{1}{1 + c}, & \text{for center of mass}\ c \geq 0\\
1 - \exp^{\frac{\log 0.5}{h}}, & \text{for half-life}\ h > 0
\frac{2}{s + 1}, & \text{for span}\ s \geq 1\\
\frac{1}{1 + c}, & \text{for center of mass}\ c \geq 0\\
1 - e^{\frac{\log 0.5}{h}}, & \text{for half-life}\ h > 0
\end{cases}

One must specify precisely one of **span**, **center of mass**, **half-life**
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ Other
- Bug in :meth:`Series.dt` methods in :class:`ArrowDtype` that were returning incorrect values. (:issue:`57355`)
- Bug in :meth:`Series.rank` that doesn't preserve missing values for nullable integers when ``na_option='keep'``. (:issue:`56976`)
- Bug in :meth:`Series.replace` and :meth:`DataFrame.replace` inconsistently replacing matching instances when ``regex=True`` and missing values are present. (:issue:`56599`)
- Bug in :meth:`Series.to_string` when series contains complex floats with exponents (:issue:`60405`)
- Bug in :meth:`read_csv` where chained fsspec TAR file and ``compression="infer"`` fails with ``tarfile.ReadError`` (:issue:`60028`)
- Bug in Dataframe Interchange Protocol implementation was returning incorrect results for data buffers' associated dtype, for string and datetime columns (:issue:`54781`)
- Bug in ``Series.list`` methods not preserving the original :class:`Index`. (:issue:`58425`)
Expand Down
4 changes: 4 additions & 0 deletions pandas/_libs/tslibs/np_datetime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ class OutOfBoundsTimedelta(ValueError):

Representation should be within a timedelta64[ns].

See Also
--------
date_range : Return a fixed frequency DatetimeIndex.

Examples
--------
>>> pd.date_range(start="1/1/1700", freq="B", periods=100000)
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,9 @@ def shift(self, periods: int = 1, fill_value: object = None) -> IntervalArray:
from pandas import Index

fill_value = Index(self._left, copy=False)._na_value
empty = IntervalArray.from_breaks([fill_value] * (empty_len + 1))
empty = IntervalArray.from_breaks(
[fill_value] * (empty_len + 1), closed=self.closed
)
else:
empty = self._from_sequence([fill_value] * empty_len, dtype=self.dtype)

Expand Down
42 changes: 42 additions & 0 deletions pandas/errors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ class UnsortedIndexError(KeyError):

Subclass of `KeyError`.

See Also
--------
DataFrame.sort_index : Sort a DataFrame by its index.
DataFrame.set_index : Set the DataFrame index using existing columns.

Examples
--------
>>> df = pd.DataFrame(
Expand Down Expand Up @@ -388,6 +393,19 @@ class DuplicateLabelError(ValueError):
"""
Error raised when an operation would introduce duplicate labels.

This error is typically encountered when performing operations on objects
with `allows_duplicate_labels=False` and the operation would result in
duplicate labels in the index. Duplicate labels can lead to ambiguities
in indexing and reduce data integrity.

See Also
--------
Series.set_flags : Return a new ``Series`` object with updated flags.
DataFrame.set_flags : Return a new ``DataFrame`` object with updated flags.
Series.reindex : Conform ``Series`` object to new index with optional filling logic.
DataFrame.reindex : Conform ``DataFrame`` object to new index with optional filling
logic.

Examples
--------
>>> s = pd.Series([0, 1, 2], index=["a", "b", "c"]).set_flags(
Expand All @@ -407,6 +425,16 @@ class InvalidIndexError(Exception):
"""
Exception raised when attempting to use an invalid index key.

This exception is triggered when a user attempts to access or manipulate
data in a pandas DataFrame or Series using an index key that is not valid
for the given object. This may occur in cases such as using a malformed
slice, a mismatched key for a ``MultiIndex``, or attempting to access an index
element that does not exist.

See Also
--------
MultiIndex : A multi-level, or hierarchical, index object for pandas objects.

Examples
--------
>>> idx = pd.MultiIndex.from_product([["x", "y"], [0, 1]])
Expand Down Expand Up @@ -510,6 +538,11 @@ class NumExprClobberingError(NameError):
to 'numexpr'. 'numexpr' is the default engine value for these methods if the
numexpr package is installed.

See Also
--------
eval : Evaluate a Python expression as a string using various backends.
DataFrame.query : Query the columns of a DataFrame with a boolean expression.

Examples
--------
>>> df = pd.DataFrame({"abs": [1, 1, 1]})
Expand Down Expand Up @@ -633,6 +666,15 @@ class PossibleDataLossError(Exception):
"""
Exception raised when trying to open a HDFStore file when already opened.

This error is triggered when there is a potential risk of data loss due to
conflicting operations on an HDFStore file. It serves to prevent unintended
overwrites or data corruption by enforcing exclusive access to the file.

See Also
--------
HDFStore : Dict-like IO interface for storing pandas objects in PyTables.
HDFStore.open : Open an HDFStore file in the specified mode.

Examples
--------
>>> store = pd.HDFStore("my-store", "a") # doctest: +SKIP
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1749,7 +1749,7 @@ def _trim_zeros_complex(str_complexes: ArrayLike, decimal: str = ".") -> list[st
# The split will give [{"", "-"}, "xxx", "+/-", "xxx", "j", ""]
# Therefore, the imaginary part is the 4th and 3rd last elements,
# and the real part is everything before the imaginary part
trimmed = re.split(r"([j+-])", x)
trimmed = re.split(r"(?<!e)([j+-])", x)
real_part.append("".join(trimmed[:-4]))
imag_part.append("".join(trimmed[-4:-2]))

Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/frame/methods/test_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,3 +757,12 @@ def test_shift_with_offsets_freq_empty(self):
df_shifted = DataFrame(index=shifted_dates)
result = df.shift(freq=offset)
tm.assert_frame_equal(result, df_shifted)

def test_series_shift_interval_preserves_closed(self):
# GH#60389
ser = Series(
[pd.Interval(1, 2, closed="right"), pd.Interval(2, 3, closed="right")]
)
result = ser.shift(1)
expected = Series([np.nan, pd.Interval(1, 2, closed="right")])
tm.assert_series_equal(result, expected)
18 changes: 18 additions & 0 deletions pandas/tests/io/formats/test_to_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,24 @@ def test_to_string_complex_float_formatting(self):
)
assert result == expected

def test_to_string_complex_float_formatting_with_exponents(self):
# GH #60393
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make this a separate test?

with option_context("display.precision", 6):
df = DataFrame(
{
"x": [
(1.8816e-09 + 0j),
(1.8816e-09 + 3.39676e-09j),
]
}
)
result = df.to_string()
expected = (
" x\n0 1.881600e-09+0.000000e+00j\n"
"1 1.881600e-09+3.396760e-09j"
)
assert result == expected

def test_to_string_format_inf(self):
# GH#24861
df = DataFrame(
Expand Down