Skip to content

add histogram method to series #23580

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 4 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ Other Enhancements
- Compatibility with Matplotlib 3.0 (:issue:`22790`).
- Added :meth:`Interval.overlaps`, :meth:`IntervalArray.overlaps`, and :meth:`IntervalIndex.overlaps` for determining overlaps between interval-like objects (:issue:`21998`)
- :meth:`Timestamp.tz_localize`, :meth:`DatetimeIndex.tz_localize`, and :meth:`Series.tz_localize` have gained the ``nonexistent`` argument for alternative handling of nonexistent times. See :ref:`timeseries.timezone_nonexsistent` (:issue:`8917`)
- Added :meth: `Series.histogram` (:pr:`23576`)

.. _whatsnew_0240.api_breaking:

Expand Down
33 changes: 33 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1888,6 +1888,39 @@ def quantile(self, q=0.5, interpolation='linear'):
# scalar
return result

def histogram(self, *args, **kwargs):
"""
Compute the histogram of a Series.

(convenience wrapper for `np.histogram`)

Parameters
----------
see `numpy.histogram`
Returns
-------
hist : array
The values of the histogram. See *density* and *weights* for a
description of the possible semantics.
bin_edges : array of dtype float
Return the bin edges `(length(hist)+1)`.

Examples
--------
>>> import numpy as np
>>> np.random.seed(3)
>>> s = pd.Series(np.random.normal(0, 1, 100))
>>> h, b = s.histogram(20)
>>> h
array([ 1, 1, 1, 1, 3, 3, 4, 10, 7, 11, 11, 7, 7, 5, 9, 7,
3, 2, 4, 3])
>>> len(b)
21

.. versionadded:: 0.24.0
"""
return np.histogram(self, *args, **kwargs)

def corr(self, other, method='pearson', min_periods=None):
"""
Compute correlation with `other` Series, excluding missing values
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/series/test_histogram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# coding=utf-8

import numpy as np
import pandas as pd


def test_histogram():
np.random.seed(3)
s = pd.Series(np.random.normal(0, 1, 100))
h, b = s.histogram(20)
_h, _b = np.histogram(s, 20)
assert np.all(h == _h)
assert np.all(b == _b)