Skip to content

Commit ab3b226

Browse files
authored
Merge branch 'main' into dev/bug/frequencyCollisions
2 parents d9bcc7f + b64f438 commit ab3b226

File tree

21 files changed

+120
-224
lines changed

21 files changed

+120
-224
lines changed

.github/CODEOWNERS

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ doc/source/development @noatamir
1010

1111
# pandas
1212
pandas/_libs/ @WillAyd
13-
pandas/_libs/tslibs/* @MarcoGorelli
1413
pandas/_typing.py @Dr-Irv
1514
pandas/core/groupby/* @rhshadrach
16-
pandas/core/tools/datetimes.py @MarcoGorelli
1715
pandas/io/excel/* @rhshadrach
1816
pandas/io/formats/style.py @attack68
1917
pandas/io/formats/style_render.py @attack68

ci/meta.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,3 @@ extra:
8989
- datapythonista
9090
- phofl
9191
- lithomas1
92-
- marcogorelli

doc/source/user_guide/basics.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ of elements to display is five, but you may pass a custom number.
3636
Attributes and underlying data
3737
------------------------------
3838

39-
pandas objects have a number of attributes enabling you to access the metadata
39+
pandas objects have a number of attributes enabling you to access the metadata.
4040

4141
* **shape**: gives the axis dimensions of the object, consistent with ndarray
4242
* Axis labels
@@ -59,7 +59,7 @@ NumPy's type system to add support for custom arrays
5959
(see :ref:`basics.dtypes`).
6060

6161
To get the actual data inside a :class:`Index` or :class:`Series`, use
62-
the ``.array`` property
62+
the ``.array`` property.
6363

6464
.. ipython:: python
6565
@@ -88,18 +88,18 @@ NumPy doesn't have a dtype to represent timezone-aware datetimes, so there
8888
are two possibly useful representations:
8989

9090
1. An object-dtype :class:`numpy.ndarray` with :class:`Timestamp` objects, each
91-
with the correct ``tz``
91+
with the correct ``tz``.
9292
2. A ``datetime64[ns]`` -dtype :class:`numpy.ndarray`, where the values have
93-
been converted to UTC and the timezone discarded
93+
been converted to UTC and the timezone discarded.
9494

95-
Timezones may be preserved with ``dtype=object``
95+
Timezones may be preserved with ``dtype=object``:
9696

9797
.. ipython:: python
9898
9999
ser = pd.Series(pd.date_range("2000", periods=2, tz="CET"))
100100
ser.to_numpy(dtype=object)
101101
102-
Or thrown away with ``dtype='datetime64[ns]'``
102+
Or thrown away with ``dtype='datetime64[ns]'``:
103103

104104
.. ipython:: python
105105

doc/source/whatsnew/v3.0.0.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ Timezones
674674

675675
Numeric
676676
^^^^^^^
677+
- Bug in :meth:`DataFrame.corr` where numerical precision errors resulted in correlations above ``1.0`` (:issue:`61120`)
677678
- Bug in :meth:`DataFrame.quantile` where the column type was not preserved when ``numeric_only=True`` with a list-like ``q`` produced an empty result (:issue:`59035`)
678679
- Bug in ``np.matmul`` with :class:`Index` inputs raising a ``TypeError`` (:issue:`57079`)
679680

@@ -773,6 +774,7 @@ Groupby/resample/rolling
773774
- Bug in :meth:`.DataFrameGroupBy.quantile` when ``interpolation="nearest"`` is inconsistent with :meth:`DataFrame.quantile` (:issue:`47942`)
774775
- Bug in :meth:`.Resampler.interpolate` on a :class:`DataFrame` with non-uniform sampling and/or indices not aligning with the resulting resampled index would result in wrong interpolation (:issue:`21351`)
775776
- Bug in :meth:`DataFrame.ewm` and :meth:`Series.ewm` when passed ``times`` and aggregation functions other than mean (:issue:`51695`)
777+
- Bug in :meth:`DataFrame.resample` changing index type to :class:`MultiIndex` when the dataframe is empty and using an upsample method (:issue:`55572`)
776778
- Bug in :meth:`DataFrameGroupBy.agg` that raises ``AttributeError`` when there is dictionary input and duplicated columns, instead of returning a DataFrame with the aggregation of all duplicate columns. (:issue:`55041`)
777779
- Bug in :meth:`DataFrameGroupBy.apply` and :meth:`SeriesGroupBy.apply` for empty data frame with ``group_keys=False`` still creating output index using group keys. (:issue:`60471`)
778780
- Bug in :meth:`DataFrameGroupBy.apply` that was returning a completely empty DataFrame when all return values of ``func`` were ``None`` instead of returning an empty DataFrame with the original columns and dtypes. (:issue:`57775`)
@@ -839,6 +841,7 @@ Other
839841
- Bug in :meth:`DataFrame.where` where using a non-bool type array in the function would return a ``ValueError`` instead of a ``TypeError`` (:issue:`56330`)
840842
- Bug in :meth:`Index.sort_values` when passing a key function that turns values into tuples, e.g. ``key=natsort.natsort_key``, would raise ``TypeError`` (:issue:`56081`)
841843
- Bug in :meth:`MultiIndex.fillna` error message was referring to ``isna`` instead of ``fillna`` (:issue:`60974`)
844+
- Bug in :meth:`Series.describe` where median percentile was always included when the ``percentiles`` argument was passed (:issue:`60550`).
842845
- Bug in :meth:`Series.diff` allowing non-integer values for the ``periods`` argument. (:issue:`56607`)
843846
- Bug in :meth:`Series.dt` methods in :class:`ArrowDtype` that were returning incorrect values. (:issue:`57355`)
844847
- Bug in :meth:`Series.isin` raising ``TypeError`` when series is large (>10**6) and ``values`` contains NA (:issue:`60678`)

pandas/_libs/algos.pyx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,9 @@ def nancorr(const float64_t[:, :] mat, bint cov=False, minp=None):
353353
float64_t[:, ::1] result
354354
uint8_t[:, :] mask
355355
int64_t nobs = 0
356-
float64_t vx, vy, dx, dy, meanx, meany, divisor, ssqdmx, ssqdmy, covxy
356+
float64_t vx, vy, dx, dy, meanx, meany, divisor, ssqdmx, ssqdmy, covxy, val
357357

358358
N, K = (<object>mat).shape
359-
360359
if minp is None:
361360
minpv = 1
362361
else:
@@ -389,8 +388,14 @@ def nancorr(const float64_t[:, :] mat, bint cov=False, minp=None):
389388
else:
390389
divisor = (nobs - 1.0) if cov else sqrt(ssqdmx * ssqdmy)
391390

391+
# clip `covxy / divisor` to ensure coeff is within bounds
392392
if divisor != 0:
393-
result[xi, yi] = result[yi, xi] = covxy / divisor
393+
val = covxy / divisor
394+
if val > 1.0:
395+
val = 1.0
396+
elif val < -1.0:
397+
val = -1.0
398+
result[xi, yi] = result[yi, xi] = val
394399
else:
395400
result[xi, yi] = result[yi, xi] = NaN
396401

pandas/_libs/tslibs/period.pyx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,9 +1752,6 @@ cdef class _Period(PeriodMixin):
17521752
def __cinit__(self, int64_t ordinal, BaseOffset freq):
17531753
self.ordinal = ordinal
17541754
self.freq = freq
1755-
# Note: this is more performant than PeriodDtype.from_date_offset(freq)
1756-
# because from_date_offset cannot be made a cdef method (until cython
1757-
# supported cdef classmethods)
17581755
self._dtype = PeriodDtypeBase(freq._period_dtype_code, freq.n)
17591756

17601757
@classmethod
@@ -1913,7 +1910,7 @@ cdef class _Period(PeriodMixin):
19131910

19141911
Parameters
19151912
----------
1916-
freq : str, BaseOffset
1913+
freq : str, DateOffset
19171914
The target frequency to convert the Period object to.
19181915
If a string is provided,
19191916
it must be a valid :ref:`period alias <timeseries.period_aliases>`.
@@ -2599,7 +2596,7 @@ cdef class _Period(PeriodMixin):
25992596
26002597
Parameters
26012598
----------
2602-
freq : str, BaseOffset
2599+
freq : str, DateOffset
26032600
Frequency to use for the returned period.
26042601
26052602
See Also

pandas/core/generic.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10818,9 +10818,8 @@ def describe(
1081810818
----------
1081910819
percentiles : list-like of numbers, optional
1082010820
The percentiles to include in the output. All should
10821-
fall between 0 and 1. The default is
10822-
``[.25, .5, .75]``, which returns the 25th, 50th, and
10823-
75th percentiles.
10821+
fall between 0 and 1. The default, ``None``, will automatically
10822+
return the 25th, 50th, and 75th percentiles.
1082410823
include : 'all', list-like of dtypes or None (default), optional
1082510824
A white list of data types to include in the result. Ignored
1082610825
for ``Series``. Here are the options:

pandas/core/methods/describe.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,15 @@ def describe_numeric_1d(series: Series, percentiles: Sequence[float]) -> Series:
229229

230230
formatted_percentiles = format_percentiles(percentiles)
231231

232+
if len(percentiles) == 0:
233+
quantiles = []
234+
else:
235+
quantiles = series.quantile(percentiles).tolist()
236+
232237
stat_index = ["count", "mean", "std", "min"] + formatted_percentiles + ["max"]
233238
d = (
234239
[series.count(), series.mean(), series.std(), series.min()]
235-
+ series.quantile(percentiles).tolist()
240+
+ quantiles
236241
+ [series.max()]
237242
)
238243
# GH#48340 - always return float on non-complex numeric data
@@ -354,10 +359,6 @@ def _refine_percentiles(
354359
# get them all to be in [0, 1]
355360
validate_percentile(percentiles)
356361

357-
# median should always be included
358-
if 0.5 not in percentiles:
359-
percentiles.append(0.5)
360-
361362
percentiles = np.asarray(percentiles)
362363

363364
# sort and check for duplicates

pandas/core/resample.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -507,22 +507,12 @@ def _wrap_result(self, result):
507507
"""
508508
Potentially wrap any results.
509509
"""
510-
# GH 47705
511-
obj = self.obj
512-
if (
513-
isinstance(result, ABCDataFrame)
514-
and len(result) == 0
515-
and not isinstance(result.index, PeriodIndex)
516-
):
517-
result = result.set_index(
518-
_asfreq_compat(obj.index[:0], freq=self.freq), append=True
519-
)
520-
521510
if isinstance(result, ABCSeries) and self._selection is not None:
522511
result.name = self._selection
523512

524513
if isinstance(result, ABCSeries) and result.empty:
525514
# When index is all NaT, result is empty but index is not
515+
obj = self.obj
526516
result.index = _asfreq_compat(obj.index[:0], freq=self.freq)
527517
result.name = getattr(obj, "name", None)
528518

@@ -1756,6 +1746,17 @@ def func(x):
17561746
return x.apply(f, *args, **kwargs)
17571747

17581748
result = self._groupby.apply(func)
1749+
1750+
# GH 47705
1751+
if (
1752+
isinstance(result, ABCDataFrame)
1753+
and len(result) == 0
1754+
and not isinstance(result.index, PeriodIndex)
1755+
):
1756+
result = result.set_index(
1757+
_asfreq_compat(self.obj.index[:0], freq=self.freq), append=True
1758+
)
1759+
17591760
return self._wrap_result(result)
17601761

17611762
_upsample = _apply

pandas/io/formats/format.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,6 +1565,9 @@ def format_percentiles(
15651565
>>> format_percentiles([0, 0.5, 0.02001, 0.5, 0.666666, 0.9999])
15661566
['0%', '50%', '2.0%', '50%', '66.67%', '99.99%']
15671567
"""
1568+
if len(percentiles) == 0:
1569+
return []
1570+
15681571
percentiles = np.asarray(percentiles)
15691572

15701573
# It checks for np.nan as well

pandas/tests/frame/methods/test_cov_corr.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,3 +485,15 @@ def test_corrwith_min_periods_boolean(self):
485485
result = df_bool.corrwith(ser_bool, min_periods=3)
486486
expected = Series([0.57735, 0.57735], index=["A", "B"])
487487
tm.assert_series_equal(result, expected)
488+
489+
def test_corr_within_bounds(self):
490+
df1 = DataFrame({"x": [0, 1], "y": [1.35951, 1.3595100000000007]})
491+
result1 = df1.corr().max().max()
492+
expected1 = 1.0
493+
tm.assert_equal(result1, expected1)
494+
495+
rng = np.random.default_rng(seed=42)
496+
df2 = DataFrame(rng.random((100, 4)))
497+
corr_matrix = df2.corr()
498+
assert corr_matrix.min().min() >= -1.0
499+
assert corr_matrix.max().max() <= 1.0

pandas/tests/frame/methods/test_describe.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,44 @@ def test_describe_exclude_pa_dtype(self):
413413
dtype=pd.ArrowDtype(pa.float64()),
414414
)
415415
tm.assert_frame_equal(result, expected)
416+
417+
@pytest.mark.parametrize("percentiles", [None, [], [0.2]])
418+
def test_refine_percentiles(self, percentiles):
419+
"""
420+
Test that the percentiles are returned correctly depending on the `percentiles`
421+
argument.
422+
- The default behavior is to return the 25th, 50th, and 75 percentiles
423+
- If `percentiles` is an empty list, no percentiles are returned
424+
- If `percentiles` is a non-empty list, only those percentiles are returned
425+
"""
426+
# GH#60550
427+
df = DataFrame({"a": np.arange(0, 10, 1)})
428+
429+
result = df.describe(percentiles=percentiles)
430+
431+
if percentiles is None:
432+
percentiles = [0.25, 0.5, 0.75]
433+
434+
expected = DataFrame(
435+
[
436+
len(df.a),
437+
df.a.mean(),
438+
df.a.std(),
439+
df.a.min(),
440+
*[df.a.quantile(p) for p in percentiles],
441+
df.a.max(),
442+
],
443+
index=pd.Index(
444+
[
445+
"count",
446+
"mean",
447+
"std",
448+
"min",
449+
*[f"{p:.0%}" for p in percentiles],
450+
"max",
451+
]
452+
),
453+
columns=["a"],
454+
)
455+
456+
tm.assert_frame_equal(result, expected)

pandas/tests/groupby/methods/test_describe.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,15 @@ def test_describe_duplicate_columns():
202202
gb = df.groupby(df[1])
203203
result = gb.describe(percentiles=[])
204204

205-
columns = ["count", "mean", "std", "min", "50%", "max"]
205+
columns = ["count", "mean", "std", "min", "max"]
206206
frames = [
207-
DataFrame([[1.0, val, np.nan, val, val, val]], index=[1], columns=columns)
207+
DataFrame([[1.0, val, np.nan, val, val]], index=[1], columns=columns)
208208
for val in (0.0, 2.0, 3.0)
209209
]
210210
expected = pd.concat(frames, axis=1)
211211
expected.columns = MultiIndex(
212212
levels=[[0, 2], columns],
213-
codes=[6 * [0] + 6 * [1] + 6 * [0], 3 * list(range(6))],
213+
codes=[5 * [0] + 5 * [1] + 5 * [0], 3 * list(range(5))],
214214
)
215215
expected.index.names = [1]
216216
tm.assert_frame_equal(result, expected)

pandas/tests/resample/test_base.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,24 @@ def test_resample_size_empty_dataframe(freq, index):
438438
tm.assert_series_equal(result, expected)
439439

440440

441+
@pytest.mark.parametrize("index", [DatetimeIndex([]), TimedeltaIndex([])])
442+
@pytest.mark.parametrize("freq", ["D", "h"])
443+
@pytest.mark.parametrize(
444+
"method", ["ffill", "bfill", "nearest", "asfreq", "interpolate", "mean"]
445+
)
446+
def test_resample_apply_empty_dataframe(index, freq, method):
447+
# GH#55572
448+
empty_frame_dti = DataFrame(index=index)
449+
450+
rs = empty_frame_dti.resample(freq)
451+
result = rs.apply(getattr(rs, method))
452+
453+
expected_index = _asfreq_compat(empty_frame_dti.index, freq)
454+
expected = DataFrame([], index=expected_index)
455+
456+
tm.assert_frame_equal(result, expected)
457+
458+
441459
@pytest.mark.parametrize(
442460
"index",
443461
[

0 commit comments

Comments
 (0)