MCPcopy Create free account
hub / github.com/apache/arrow / _check_temporal_rounding

Function _check_temporal_rounding

python/pyarrow/tests/test_compute.py:2842–2943  ·  view source on GitHub ↗
(ts, values, unit)

Source from the content-addressed store, hash-verified

2840
2841
2842def _check_temporal_rounding(ts, values, unit):
2843 unit_shorthand = {
2844 "nanosecond": "ns",
2845 "microsecond": "us",
2846 "millisecond": "ms",
2847 "second": "s",
2848 "minute": "min",
2849 "hour": "h",
2850 "day": "D"
2851 }
2852 greater_unit = {
2853 "nanosecond": "us",
2854 "microsecond": "ms",
2855 "millisecond": "s",
2856 "second": "min",
2857 "minute": "h",
2858 "hour": "D",
2859 }
2860 ta = pa.array(ts)
2861
2862 for value in values:
2863 frequency = str(value) + unit_shorthand[unit]
2864 options = pc.RoundTemporalOptions(value, unit)
2865
2866 result = pc.ceil_temporal(ta, options=options).to_pandas()
2867 expected = ts.dt.ceil(frequency)
2868 np.testing.assert_array_equal(result, expected)
2869
2870 result = pc.floor_temporal(ta, options=options).to_pandas()
2871 expected = ts.dt.floor(frequency)
2872 np.testing.assert_array_equal(result, expected)
2873
2874 result = pc.round_temporal(ta, options=options).to_pandas()
2875 expected = ts.dt.round(frequency)
2876 np.testing.assert_array_equal(result, expected)
2877
2878 # Check rounding with calendar_based_origin=True.
2879 # Note: rounding to month is not supported in Pandas so we can't
2880 # approximate this functionality and exclude unit == "day".
2881 if unit != "day":
2882 options = pc.RoundTemporalOptions(
2883 value, unit, calendar_based_origin=True)
2884 origin = ts.dt.floor(greater_unit[unit])
2885
2886 if ta.type.tz is None:
2887 result = pc.ceil_temporal(ta, options=options).to_pandas()
2888 expected = (ts - origin).dt.ceil(frequency) + origin
2889 np.testing.assert_array_equal(result, expected)
2890
2891 result = pc.floor_temporal(ta, options=options).to_pandas()
2892 expected = (ts - origin).dt.floor(frequency) + origin
2893 np.testing.assert_array_equal(result, expected)
2894
2895 result = pc.round_temporal(ta, options=options).to_pandas()
2896 expected = (ts - origin).dt.round(frequency) + origin
2897 np.testing.assert_array_equal(result, expected)
2898
2899 # Check RoundTemporalOptions partial defaults

Callers 1

test_round_temporalFunction · 0.85

Calls 2

RoundTemporalOptionsMethod · 0.80
arrayMethod · 0.45

Tested by

no test coverage detected