Skip to content

Commit c7acef1

Browse files
committed
merged with master
2 parents 6fd15c9 + 7d0ee96 commit c7acef1

24 files changed

+208
-72
lines changed

doc/redirects.csv

+5-5
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,11 @@ generated/pandas.core.resample.Resampler.std,../reference/api/pandas.core.resamp
269269
generated/pandas.core.resample.Resampler.sum,../reference/api/pandas.core.resample.Resampler.sum
270270
generated/pandas.core.resample.Resampler.transform,../reference/api/pandas.core.resample.Resampler.transform
271271
generated/pandas.core.resample.Resampler.var,../reference/api/pandas.core.resample.Resampler.var
272-
generated/pandas.core.window.EWM.corr,../reference/api/pandas.core.window.EWM.corr
273-
generated/pandas.core.window.EWM.cov,../reference/api/pandas.core.window.EWM.cov
274-
generated/pandas.core.window.EWM.mean,../reference/api/pandas.core.window.EWM.mean
275-
generated/pandas.core.window.EWM.std,../reference/api/pandas.core.window.EWM.std
276-
generated/pandas.core.window.EWM.var,../reference/api/pandas.core.window.EWM.var
272+
generated/pandas.core.window.ExponentialMovingWindow.corr,../reference/api/pandas.core.window.ExponentialMovingWindow.corr
273+
generated/pandas.core.window.ExponentialMovingWindow.cov,../reference/api/pandas.core.window.ExponentialMovingWindow.cov
274+
generated/pandas.core.window.ExponentialMovingWindow.mean,../reference/api/pandas.core.window.ExponentialMovingWindow.mean
275+
generated/pandas.core.window.ExponentialMovingWindow.std,../reference/api/pandas.core.window.ExponentialMovingWindow.std
276+
generated/pandas.core.window.ExponentialMovingWindow.var,../reference/api/pandas.core.window.ExponentialMovingWindow.var
277277
generated/pandas.core.window.Expanding.aggregate,../reference/api/pandas.core.window.Expanding.aggregate
278278
generated/pandas.core.window.Expanding.apply,../reference/api/pandas.core.window.Expanding.apply
279279
generated/pandas.core.window.Expanding.corr,../reference/api/pandas.core.window.Expanding.corr

doc/source/reference/window.rst

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Window
88

99
Rolling objects are returned by ``.rolling`` calls: :func:`pandas.DataFrame.rolling`, :func:`pandas.Series.rolling`, etc.
1010
Expanding objects are returned by ``.expanding`` calls: :func:`pandas.DataFrame.expanding`, :func:`pandas.Series.expanding`, etc.
11-
EWM objects are returned by ``.ewm`` calls: :func:`pandas.DataFrame.ewm`, :func:`pandas.Series.ewm`, etc.
11+
ExponentialMovingWindow objects are returned by ``.ewm`` calls: :func:`pandas.DataFrame.ewm`, :func:`pandas.Series.ewm`, etc.
1212

1313
Standard moving window functions
1414
--------------------------------
@@ -69,11 +69,11 @@ Exponentially-weighted moving window functions
6969
.. autosummary::
7070
:toctree: api/
7171

72-
EWM.mean
73-
EWM.std
74-
EWM.var
75-
EWM.corr
76-
EWM.cov
72+
ExponentialMovingWindow.mean
73+
ExponentialMovingWindow.std
74+
ExponentialMovingWindow.var
75+
ExponentialMovingWindow.corr
76+
ExponentialMovingWindow.cov
7777

7878
Window indexer
7979
--------------

doc/source/user_guide/computation.rst

+10-10
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ see the :ref:`groupby docs <groupby.transform.window_resample>`.
230230
The API for window statistics is quite similar to the way one works with ``GroupBy`` objects, see the documentation :ref:`here <groupby>`.
231231

232232
We work with ``rolling``, ``expanding`` and ``exponentially weighted`` data through the corresponding
233-
objects, :class:`~pandas.core.window.Rolling`, :class:`~pandas.core.window.Expanding` and :class:`~pandas.core.window.EWM`.
233+
objects, :class:`~pandas.core.window.Rolling`, :class:`~pandas.core.window.Expanding` and :class:`~pandas.core.window.ExponentialMovingWindow`.
234234

235235
.. ipython:: python
236236
@@ -777,7 +777,7 @@ columns by reshaping and indexing:
777777
Aggregation
778778
-----------
779779

780-
Once the ``Rolling``, ``Expanding`` or ``EWM`` objects have been created, several methods are available to
780+
Once the ``Rolling``, ``Expanding`` or ``ExponentialMovingWindow`` objects have been created, several methods are available to
781781
perform multiple computations on the data. These operations are similar to the :ref:`aggregating API <basics.aggregate>`,
782782
:ref:`groupby API <groupby.aggregate>`, and :ref:`resample API <timeseries.aggregate>`.
783783

@@ -971,7 +971,7 @@ Exponentially weighted windows
971971

972972
A related set of functions are exponentially weighted versions of several of
973973
the above statistics. A similar interface to ``.rolling`` and ``.expanding`` is accessed
974-
through the ``.ewm`` method to receive an :class:`~EWM` object.
974+
through the ``.ewm`` method to receive an :class:`~ExponentialMovingWindow` object.
975975
A number of expanding EW (exponentially weighted)
976976
methods are provided:
977977

@@ -980,11 +980,11 @@ methods are provided:
980980
:header: "Function", "Description"
981981
:widths: 20, 80
982982

983-
:meth:`~EWM.mean`, EW moving average
984-
:meth:`~EWM.var`, EW moving variance
985-
:meth:`~EWM.std`, EW moving standard deviation
986-
:meth:`~EWM.corr`, EW moving correlation
987-
:meth:`~EWM.cov`, EW moving covariance
983+
:meth:`~ExponentialMovingWindow.mean`, EW moving average
984+
:meth:`~ExponentialMovingWindow.var`, EW moving variance
985+
:meth:`~ExponentialMovingWindow.std`, EW moving standard deviation
986+
:meth:`~ExponentialMovingWindow.corr`, EW moving correlation
987+
:meth:`~ExponentialMovingWindow.cov`, EW moving covariance
988988

989989
In general, a weighted moving average is calculated as
990990

@@ -1090,12 +1090,12 @@ Here is an example for a univariate time series:
10901090
@savefig ewma_ex.png
10911091
s.ewm(span=20).mean().plot(style='k')
10921092
1093-
EWM has a ``min_periods`` argument, which has the same
1093+
ExponentialMovingWindow has a ``min_periods`` argument, which has the same
10941094
meaning it does for all the ``.expanding`` and ``.rolling`` methods:
10951095
no output values will be set until at least ``min_periods`` non-null values
10961096
are encountered in the (expanding) window.
10971097

1098-
EWM also has an ``ignore_na`` argument, which determines how
1098+
ExponentialMovingWindow also has an ``ignore_na`` argument, which determines how
10991099
intermediate null values affect the calculation of the weights.
11001100
When ``ignore_na=False`` (the default), weights are calculated based on absolute
11011101
positions, so that intermediate null values affect the result.

doc/source/user_guide/cookbook.rst

+19
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,25 @@ Storing Attributes to a group node
11661166
store.close()
11671167
os.remove('test.h5')
11681168
1169+
You can create or load a HDFStore in-memory by passing the ``driver``
1170+
parameter to PyTables. Changes are only written to disk when the HDFStore
1171+
is closed.
1172+
1173+
.. ipython:: python
1174+
1175+
store = pd.HDFStore('test.h5', 'w', diver='H5FD_CORE')
1176+
1177+
df = pd.DataFrame(np.random.randn(8, 3))
1178+
store['test'] = df
1179+
1180+
# only after closing the store, data is written to disk:
1181+
store.close()
1182+
1183+
.. ipython:: python
1184+
:suppress:
1185+
1186+
os.remove('test.h5')
1187+
11691188
.. _cookbook.binary:
11701189

11711190
Binary files

doc/source/user_guide/timeseries.rst

+8
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ inferred frequency upon creation:
235235
236236
pd.DatetimeIndex(['2018-01-01', '2018-01-03', '2018-01-05'], freq='infer')
237237
238+
.. _timeseries.converting.format:
239+
238240
Providing a format argument
239241
~~~~~~~~~~~~~~~~~~~~~~~~~~~
240242

@@ -319,6 +321,12 @@ which can be specified. These are computed from the starting point specified by
319321
pd.to_datetime([1349720105100, 1349720105200, 1349720105300,
320322
1349720105400, 1349720105500], unit='ms')
321323
324+
.. note::
325+
326+
The ``unit`` parameter does not use the same strings as the ``format`` parameter
327+
that was discussed :ref:`above<timeseries.converting.format>`). The
328+
available units are listed on the documentation for :func:`pandas.to_datetime`.
329+
322330
Constructing a :class:`Timestamp` or :class:`DatetimeIndex` with an epoch timestamp
323331
with the ``tz`` argument specified will currently localize the epoch timestamps to UTC
324332
first then convert the result to the specified time zone. However, this behavior

doc/source/user_guide/visualization.rst

+6-5
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,7 @@ Here is an example of one way to easily plot group means with standard deviation
14231423
# Plot
14241424
fig, ax = plt.subplots()
14251425
@savefig errorbar_example.png
1426-
means.plot.bar(yerr=errors, ax=ax, capsize=4)
1426+
means.plot.bar(yerr=errors, ax=ax, capsize=4, rot=0)
14271427
14281428
.. ipython:: python
14291429
:suppress:
@@ -1444,9 +1444,9 @@ Plotting with matplotlib table is now supported in :meth:`DataFrame.plot` and :
14441444
14451445
.. ipython:: python
14461446
1447-
fig, ax = plt.subplots(1, 1)
1447+
fig, ax = plt.subplots(1, 1, figsize=(7, 6.5))
14481448
df = pd.DataFrame(np.random.rand(5, 3), columns=['a', 'b', 'c'])
1449-
ax.get_xaxis().set_visible(False) # Hide Ticks
1449+
ax.xaxis.tick_top() # Display x-axis ticks on top.
14501450
14511451
@savefig line_plot_table_true.png
14521452
df.plot(table=True, ax=ax)
@@ -1463,8 +1463,9 @@ as seen in the example below.
14631463

14641464
.. ipython:: python
14651465
1466-
fig, ax = plt.subplots(1, 1)
1467-
ax.get_xaxis().set_visible(False) # Hide Ticks
1466+
fig, ax = plt.subplots(1, 1, figsize=(7, 6.75))
1467+
ax.xaxis.tick_top() # Display x-axis ticks on top.
1468+
14681469
@savefig line_plot_table_data.png
14691470
df.plot(table=np.round(df.T, 2), ax=ax)
14701471

doc/source/whatsnew/v0.25.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,7 @@ Groupby/resample/rolling
12061206
- Bug in :meth:`pandas.core.groupby.GroupBy.agg` where incorrect results are returned for uint64 columns. (:issue:`26310`)
12071207
- Bug in :meth:`pandas.core.window.Rolling.median` and :meth:`pandas.core.window.Rolling.quantile` where MemoryError is raised with empty window (:issue:`26005`)
12081208
- Bug in :meth:`pandas.core.window.Rolling.median` and :meth:`pandas.core.window.Rolling.quantile` where incorrect results are returned with ``closed='left'`` and ``closed='neither'`` (:issue:`26005`)
1209-
- Improved :class:`pandas.core.window.Rolling`, :class:`pandas.core.window.Window` and :class:`pandas.core.window.EWM` functions to exclude nuisance columns from results instead of raising errors and raise a ``DataError`` only if all columns are nuisance (:issue:`12537`)
1209+
- Improved :class:`pandas.core.window.Rolling`, :class:`pandas.core.window.Window` and :class:`pandas.core.window.ExponentialMovingWindow` functions to exclude nuisance columns from results instead of raising errors and raise a ``DataError`` only if all columns are nuisance (:issue:`12537`)
12101210
- Bug in :meth:`pandas.core.window.Rolling.max` and :meth:`pandas.core.window.Rolling.min` where incorrect results are returned with an empty variable window (:issue:`26005`)
12111211
- Raise a helpful exception when an unsupported weighted window function is used as an argument of :meth:`pandas.core.window.Window.aggregate` (:issue:`26597`)
12121212

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,7 @@ MultiIndex
956956
df.loc[(['b', 'a'], [2, 1]), :]
957957
958958
- Bug in :meth:`MultiIndex.intersection` was not guaranteed to preserve order when ``sort=False``. (:issue:`31325`)
959+
- Bug in :meth:`DataFrame.truncate` was dropping :class:`MultiIndex` names. (:issue:`34564`)
959960

960961
.. ipython:: python
961962

pandas/core/apply.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,7 @@ def apply_empty_result(self):
220220

221221
def apply_raw(self):
222222
""" apply to the values as a numpy array """
223-
result, reduction_success = libreduction.compute_reduction(
224-
self.values, self.f, axis=self.axis
225-
)
226-
227-
# We expect np.apply_along_axis to give a two-dimensional result, or raise.
228-
if not reduction_success:
229-
result = np.apply_along_axis(self.f, self.axis, self.values)
223+
result = np.apply_along_axis(self.f, self.axis, self.values)
230224

231225
# TODO: mixed type case
232226
if result.ndim == 2:

pandas/core/frame.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7288,7 +7288,7 @@ def _gotitem(
72887288
core.resample.Resampler : Perform operations over resampled bins.
72897289
core.window.Rolling : Perform operations over rolling window.
72907290
core.window.Expanding : Perform operations over expanding window.
7291-
core.window.EWM : Perform operation over exponential weighted
7291+
core.window.ExponentialMovingWindow : Perform operation over exponential weighted
72927292
window.
72937293
"""
72947294
)
@@ -8171,7 +8171,7 @@ def cov(self, min_periods=None) -> "DataFrame":
81718171
See Also
81728172
--------
81738173
Series.cov : Compute covariance with another Series.
8174-
core.window.EWM.cov: Exponential weighted sample covariance.
8174+
core.window.ExponentialMovingWindow.cov: Exponential weighted sample covariance.
81758175
core.window.Expanding.cov : Expanding sample covariance.
81768176
core.window.Rolling.cov : Rolling sample covariance.
81778177

pandas/core/generic.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -10460,7 +10460,12 @@ def _add_series_or_dataframe_operations(cls):
1046010460
Add the series or dataframe only operations to the cls; evaluate
1046110461
the doc strings again.
1046210462
"""
10463-
from pandas.core.window import EWM, Expanding, Rolling, Window
10463+
from pandas.core.window import (
10464+
Expanding,
10465+
ExponentialMovingWindow,
10466+
Rolling,
10467+
Window,
10468+
)
1046410469

1046510470
@doc(Rolling)
1046610471
def rolling(
@@ -10507,7 +10512,7 @@ def expanding(self, min_periods=1, center=False, axis=0):
1050710512

1050810513
cls.expanding = expanding
1050910514

10510-
@doc(EWM)
10515+
@doc(ExponentialMovingWindow)
1051110516
def ewm(
1051210517
self,
1051310518
com=None,
@@ -10520,7 +10525,7 @@ def ewm(
1052010525
axis=0,
1052110526
):
1052210527
axis = self._get_axis_number(axis)
10523-
return EWM(
10528+
return ExponentialMovingWindow(
1052410529
self,
1052510530
com=com,
1052610531
span=span,

pandas/core/indexes/multi.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -3193,7 +3193,12 @@ def truncate(self, before=None, after=None):
31933193
new_codes = [level_codes[left:right] for level_codes in self.codes]
31943194
new_codes[0] = new_codes[0] - i
31953195

3196-
return MultiIndex(levels=new_levels, codes=new_codes, verify_integrity=False)
3196+
return MultiIndex(
3197+
levels=new_levels,
3198+
codes=new_codes,
3199+
names=self._names,
3200+
verify_integrity=False,
3201+
)
31973202

31983203
def equals(self, other) -> bool:
31993204
"""

pandas/core/window/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from pandas.core.window.ewm import EWM # noqa:F401
1+
from pandas.core.window.ewm import ExponentialMovingWindow # noqa:F401
22
from pandas.core.window.expanding import Expanding, ExpandingGroupby # noqa:F401
33
from pandas.core.window.rolling import Rolling, RollingGroupby, Window # noqa:F401

pandas/core/window/ewm.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def get_center_of_mass(
5959
return float(comass)
6060

6161

62-
class EWM(_Rolling):
62+
class ExponentialMovingWindow(_Rolling):
6363
r"""
6464
Provide exponential weighted (EW) functions.
6565
@@ -185,7 +185,7 @@ def __init__(
185185

186186
@property
187187
def _constructor(self):
188-
return EWM
188+
return ExponentialMovingWindow
189189

190190
_agg_see_also_doc = dedent(
191191
"""

pandas/io/pytables.py

+27-12
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,8 @@ class HDFStore:
447447
448448
Parameters
449449
----------
450-
path : string
451-
File path to HDF5 file
450+
path : str
451+
File path to HDF5 file.
452452
mode : {'a', 'w', 'r', 'r+'}, default 'a'
453453
454454
``'r'``
@@ -462,18 +462,20 @@ class HDFStore:
462462
``'r+'``
463463
It is similar to ``'a'``, but the file must already exist.
464464
complevel : int, 0-9, default None
465-
Specifies a compression level for data.
466-
A value of 0 or None disables compression.
465+
Specifies a compression level for data.
466+
A value of 0 or None disables compression.
467467
complib : {'zlib', 'lzo', 'bzip2', 'blosc'}, default 'zlib'
468-
Specifies the compression library to be used.
469-
As of v0.20.2 these additional compressors for Blosc are supported
470-
(default if no compressor specified: 'blosc:blosclz'):
471-
{'blosc:blosclz', 'blosc:lz4', 'blosc:lz4hc', 'blosc:snappy',
472-
'blosc:zlib', 'blosc:zstd'}.
473-
Specifying a compression library which is not available issues
474-
a ValueError.
468+
Specifies the compression library to be used.
469+
As of v0.20.2 these additional compressors for Blosc are supported
470+
(default if no compressor specified: 'blosc:blosclz'):
471+
{'blosc:blosclz', 'blosc:lz4', 'blosc:lz4hc', 'blosc:snappy',
472+
'blosc:zlib', 'blosc:zstd'}.
473+
Specifying a compression library which is not available issues
474+
a ValueError.
475475
fletcher32 : bool, default False
476-
If applying compression use the fletcher32 checksum
476+
If applying compression use the fletcher32 checksum.
477+
**kwargs
478+
These parameters will be passed to the PyTables open_file method.
477479
478480
Examples
479481
--------
@@ -482,6 +484,17 @@ class HDFStore:
482484
>>> store['foo'] = bar # write to HDF5
483485
>>> bar = store['foo'] # retrieve
484486
>>> store.close()
487+
488+
**Create or load HDF5 file in-memory**
489+
490+
When passing the `driver` option to the PyTables open_file method through
491+
**kwargs, the HDF5 file is loaded or created in-memory and will only be
492+
written when closed:
493+
494+
>>> bar = pd.DataFrame(np.random.randn(10, 4))
495+
>>> store = pd.HDFStore('test.h5', driver='H5FD_CORE')
496+
>>> store['foo'] = bar
497+
>>> store.close() # only now, data is written to disk
485498
"""
486499

487500
_handle: Optional["File"]
@@ -634,6 +647,8 @@ def open(self, mode: str = "a", **kwargs):
634647
----------
635648
mode : {'a', 'w', 'r', 'r+'}, default 'a'
636649
See HDFStore docstring or tables.open_file for info about modes
650+
**kwargs
651+
These parameters will be passed to the PyTables open_file method.
637652
"""
638653
tables = _tables()
639654

pandas/tests/frame/methods/test_truncate.py

+13
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,16 @@ def test_truncate_decreasing_index(self, before, after, indices, klass):
104104
result = values.truncate(before=before, after=after)
105105
expected = values.loc[indices]
106106
tm.assert_frame_equal(result, expected)
107+
108+
def test_truncate_multiindex(self):
109+
# GH 34564
110+
mi = pd.MultiIndex.from_product([[1, 2, 3, 4], ["A", "B"]], names=["L1", "L2"])
111+
s1 = pd.DataFrame(range(mi.shape[0]), index=mi, columns=["col"])
112+
result = s1.truncate(before=2, after=3)
113+
114+
df = pd.DataFrame.from_dict(
115+
{"L1": [2, 2, 3, 3], "L2": ["A", "B", "A", "B"], "col": [2, 3, 4, 5]}
116+
)
117+
expected = df.set_index(["L1", "L2"])
118+
119+
tm.assert_frame_equal(result, expected)

pandas/tests/frame/test_apply.py

-3
Original file line numberDiff line numberDiff line change
@@ -745,9 +745,6 @@ def non_reducing_function(row):
745745
df.apply(func, axis=1)
746746
assert names == list(df.index)
747747

748-
@pytest.mark.xfail(
749-
reason="The 'run once' enhancement for apply_raw not implemented yet."
750-
)
751748
def test_apply_raw_function_runs_once(self):
752749
# https://github.com/pandas-dev/pandas/issues/34506
753750

0 commit comments

Comments
 (0)