File: test_pandaslike.py

package info (click to toggle)
python-boost-histogram 1.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,236 kB
  • sloc: python: 7,940; cpp: 3,243; makefile: 22; sh: 1
file content (35 lines) | stat: -rw-r--r-- 1,107 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from __future__ import annotations

import numpy as np

import boost_histogram as bh


# This is not a reasonable approximation of Pandas, but rather a test for an
# arbitrary convertible object (but if this works, so does Pandas)
class Seriesish:
    def __init__(self, array):
        self.array = np.asarray(array)

    def __array__(self, dtype=None, *, copy=None):
        # Copy is new in NumPy 2.0
        kw = {}
        if copy is not None:
            kw["copy"] = copy
        return np.asarray(self.array, dtype=dtype, **kw)


def test_setting_weighted_profile_convertable():
    h = bh.Histogram(bh.axis.Regular(10, 0, 10), storage=bh.storage.WeightedMean())
    data = Seriesish([0.3, 0.3, 0.4, 1.2, 1.6])
    samples = Seriesish([1, 2, 3, 4, 4])
    weights = Seriesish([1, 1, 1, 1, 2])

    h.fill(data, sample=samples, weight=weights)

    assert h[0] == bh.accumulators.WeightedMean(
        sum_of_weights=3, sum_of_weights_squared=3, value=2, variance=1
    )
    assert h[1] == bh.accumulators.WeightedMean(
        sum_of_weights=3, sum_of_weights_squared=5, value=4, variance=0
    )