Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/pandas/conftest.py
7799 views
"""1This file is very long and growing, but it was decided to not split it yet, as2it's still manageable (2020-03-17, ~1.1k LoC). See gh-3198934Instead of splitting it was decided to define sections here:5- Configuration / Settings6- Autouse fixtures7- Common arguments8- Missing values & co.9- Classes10- Indices11- Series'12- DataFrames13- Operators & Operations14- Data sets/files15- Time zones16- Dtypes17- Misc18"""19# pyright: reportUntypedFunctionDecorator = false2021from collections import abc22from datetime import (23date,24datetime,25time,26timedelta,27timezone,28)29from decimal import Decimal30import operator31import os3233from dateutil.tz import (34tzlocal,35tzutc,36)37import hypothesis38from hypothesis import strategies as st39import numpy as np40import pytest41from pytz import (42FixedOffset,43utc,44)4546import pandas.util._test_decorators as td4748from pandas.core.dtypes.dtypes import (49DatetimeTZDtype,50IntervalDtype,51)5253import pandas as pd54from pandas import (55DataFrame,56Interval,57Period,58Series,59Timedelta,60Timestamp,61)62import pandas._testing as tm63from pandas.core import ops64from pandas.core.indexes.api import (65Index,66MultiIndex,67)6869try:70import pyarrow as pa71except ImportError:72has_pyarrow = False73else:74del pa75has_pyarrow = True7677# Until https://github.com/numpy/numpy/issues/19078 is sorted out, just suppress78suppress_npdev_promotion_warning = pytest.mark.filterwarnings(79"ignore:Promotion of numbers and bools:FutureWarning"80)8182# ----------------------------------------------------------------83# Configuration / Settings84# ----------------------------------------------------------------85# pytest868788def pytest_addoption(parser):89parser.addoption("--skip-slow", action="store_true", help="skip slow tests")90parser.addoption("--skip-network", action="store_true", help="skip network tests")91parser.addoption("--skip-db", action="store_true", help="skip db tests")92parser.addoption(93"--run-high-memory", action="store_true", help="run high memory tests"94)95parser.addoption("--only-slow", action="store_true", help="run only slow tests")96parser.addoption(97"--strict-data-files",98action="store_true",99help="Fail if a test is skipped for missing data file.",100)101102103def pytest_collection_modifyitems(items, config):104skip_slow = config.getoption("--skip-slow")105only_slow = config.getoption("--only-slow")106skip_network = config.getoption("--skip-network")107skip_db = config.getoption("--skip-db")108run_high_memory = config.getoption("--run-high-memory")109110marks = [111(pytest.mark.slow, "slow", skip_slow, "--skip-slow"),112(pytest.mark.network, "network", skip_network, "--network"),113(pytest.mark.db, "db", skip_db, "--skip-db"),114]115116for item in items:117if config.getoption("--doctest-modules") or config.getoption(118"--doctest-cython", default=False119):120# autouse=True for the add_doctest_imports can lead to expensive teardowns121# since doctest_namespace is a session fixture122item.add_marker(pytest.mark.usefixtures("add_doctest_imports"))123# mark all tests in the pandas/tests/frame directory with "arraymanager"124if "/frame/" in item.nodeid:125item.add_marker(pytest.mark.arraymanager)126item.add_marker(suppress_npdev_promotion_warning)127128for (mark, kwd, skip_if_found, arg_name) in marks:129if kwd in item.keywords:130# If we're skipping, no need to actually add the marker or look for131# other markers132if skip_if_found:133item.add_marker(pytest.mark.skip(f"skipping due to {arg_name}"))134break135136item.add_marker(mark)137138if only_slow and "slow" not in item.keywords:139item.add_marker(pytest.mark.skip("skipping due to --only-slow"))140141if "high_memory" in item.keywords and not run_high_memory:142item.add_marker(143pytest.mark.skip(144"skipping high memory test since --run-high-memory was not set"145)146)147148149# Hypothesis150hypothesis.settings.register_profile(151"ci",152# Hypothesis timing checks are tuned for scalars by default, so we bump153# them from 200ms to 500ms per test case as the global default. If this154# is too short for a specific test, (a) try to make it faster, and (b)155# if it really is slow add `@settings(deadline=...)` with a working value,156# or `deadline=None` to entirely disable timeouts for that test.157# 2022-02-09: Changed deadline from 500 -> None. Deadline leads to158# non-actionable, flaky CI failures (# GH 24641, 44969, 45118, 44969)159deadline=None,160suppress_health_check=(hypothesis.HealthCheck.too_slow,),161)162hypothesis.settings.load_profile("ci")163164# Registering these strategies makes them globally available via st.from_type,165# which is use for offsets in tests/tseries/offsets/test_offsets_properties.py166for name in "MonthBegin MonthEnd BMonthBegin BMonthEnd".split():167cls = getattr(pd.tseries.offsets, name)168st.register_type_strategy(169cls, st.builds(cls, n=st.integers(-99, 99), normalize=st.booleans())170)171172for name in "YearBegin YearEnd BYearBegin BYearEnd".split():173cls = getattr(pd.tseries.offsets, name)174st.register_type_strategy(175cls,176st.builds(177cls,178n=st.integers(-5, 5),179normalize=st.booleans(),180month=st.integers(min_value=1, max_value=12),181),182)183184for name in "QuarterBegin QuarterEnd BQuarterBegin BQuarterEnd".split():185cls = getattr(pd.tseries.offsets, name)186st.register_type_strategy(187cls,188st.builds(189cls,190n=st.integers(-24, 24),191normalize=st.booleans(),192startingMonth=st.integers(min_value=1, max_value=12),193),194)195196197@pytest.fixture198def add_doctest_imports(doctest_namespace):199"""200Make `np` and `pd` names available for doctests.201"""202doctest_namespace["np"] = np203doctest_namespace["pd"] = pd204205206# ----------------------------------------------------------------207# Autouse fixtures208# ----------------------------------------------------------------209@pytest.fixture(autouse=True)210def configure_tests():211"""212Configure settings for all tests and test modules.213"""214pd.set_option("chained_assignment", "raise")215216217# ----------------------------------------------------------------218# Common arguments219# ----------------------------------------------------------------220@pytest.fixture(params=[0, 1, "index", "columns"], ids=lambda x: f"axis={repr(x)}")221def axis(request):222"""223Fixture for returning the axis numbers of a DataFrame.224"""225return request.param226227228axis_frame = axis229230231@pytest.fixture(params=[True, False, None])232def observed(request):233"""234Pass in the observed keyword to groupby for [True, False]235This indicates whether categoricals should return values for236values which are not in the grouper [False / None], or only values which237appear in the grouper [True]. [None] is supported for future compatibility238if we decide to change the default (and would need to warn if this239parameter is not passed).240"""241return request.param242243244@pytest.fixture(params=[True, False, None])245def ordered(request):246"""247Boolean 'ordered' parameter for Categorical.248"""249return request.param250251252@pytest.fixture(params=["first", "last", False])253def keep(request):254"""255Valid values for the 'keep' parameter used in256.duplicated or .drop_duplicates257"""258return request.param259260261@pytest.fixture(params=["both", "neither", "left", "right"])262def inclusive_endpoints_fixture(request):263"""264Fixture for trying all interval 'inclusive' parameters.265"""266return request.param267268269@pytest.fixture(params=["left", "right", "both", "neither"])270def closed(request):271"""272Fixture for trying all interval closed parameters.273"""274return request.param275276277@pytest.fixture(params=["left", "right", "both", "neither"])278def other_closed(request):279"""280Secondary closed fixture to allow parametrizing over all pairs of closed.281"""282return request.param283284285@pytest.fixture(286params=[287None,288"gzip",289"bz2",290"zip",291"xz",292pytest.param("zstd", marks=td.skip_if_no("zstandard")),293]294)295def compression(request):296"""297Fixture for trying common compression types in compression tests.298"""299return request.param300301302@pytest.fixture(303params=[304"gzip",305"bz2",306"zip",307"xz",308pytest.param("zstd", marks=td.skip_if_no("zstandard")),309]310)311def compression_only(request):312"""313Fixture for trying common compression types in compression tests excluding314uncompressed case.315"""316return request.param317318319@pytest.fixture(params=[True, False])320def writable(request):321"""322Fixture that an array is writable.323"""324return request.param325326327@pytest.fixture(params=["inner", "outer", "left", "right"])328def join_type(request):329"""330Fixture for trying all types of join operations.331"""332return request.param333334335@pytest.fixture(params=["nlargest", "nsmallest"])336def nselect_method(request):337"""338Fixture for trying all nselect methods.339"""340return request.param341342343# ----------------------------------------------------------------344# Missing values & co.345# ----------------------------------------------------------------346@pytest.fixture(params=tm.NULL_OBJECTS, ids=lambda x: type(x).__name__)347def nulls_fixture(request):348"""349Fixture for each null type in pandas.350"""351return request.param352353354nulls_fixture2 = nulls_fixture # Generate cartesian product of nulls_fixture355356357@pytest.fixture(params=[None, np.nan, pd.NaT])358def unique_nulls_fixture(request):359"""360Fixture for each null type in pandas, each null type exactly once.361"""362return request.param363364365# Generate cartesian product of unique_nulls_fixture:366unique_nulls_fixture2 = unique_nulls_fixture367368369@pytest.fixture(params=tm.NP_NAT_OBJECTS, ids=lambda x: type(x).__name__)370def np_nat_fixture(request):371"""372Fixture for each NaT type in numpy.373"""374return request.param375376377# Generate cartesian product of np_nat_fixture:378np_nat_fixture2 = np_nat_fixture379380381# ----------------------------------------------------------------382# Classes383# ----------------------------------------------------------------384385386@pytest.fixture(params=[DataFrame, Series])387def frame_or_series(request):388"""389Fixture to parametrize over DataFrame and Series.390"""391return request.param392393394# error: List item 0 has incompatible type "Type[Index]"; expected "Type[IndexOpsMixin]"395@pytest.fixture(396params=[Index, Series], ids=["index", "series"] # type: ignore[list-item]397)398def index_or_series(request):399"""400Fixture to parametrize over Index and Series, made necessary by a mypy401bug, giving an error:402403List item 0 has incompatible type "Type[Series]"; expected "Type[PandasObject]"404405See GH#29725406"""407return request.param408409410# Generate cartesian product of index_or_series fixture:411index_or_series2 = index_or_series412413414@pytest.fixture(params=[Index, Series, pd.array], ids=["index", "series", "array"])415def index_or_series_or_array(request):416"""417Fixture to parametrize over Index, Series, and ExtensionArray418"""419return request.param420421422@pytest.fixture(params=[Index, Series, DataFrame, pd.array], ids=lambda x: x.__name__)423def box_with_array(request):424"""425Fixture to test behavior for Index, Series, DataFrame, and pandas Array426classes427"""428return request.param429430431box_with_array2 = box_with_array432433434@pytest.fixture435def dict_subclass():436"""437Fixture for a dictionary subclass.438"""439440class TestSubDict(dict):441def __init__(self, *args, **kwargs):442dict.__init__(self, *args, **kwargs)443444return TestSubDict445446447@pytest.fixture448def non_dict_mapping_subclass():449"""450Fixture for a non-mapping dictionary subclass.451"""452453class TestNonDictMapping(abc.Mapping):454def __init__(self, underlying_dict):455self._data = underlying_dict456457def __getitem__(self, key):458return self._data.__getitem__(key)459460def __iter__(self):461return self._data.__iter__()462463def __len__(self):464return self._data.__len__()465466return TestNonDictMapping467468469# ----------------------------------------------------------------470# Indices471# ----------------------------------------------------------------472@pytest.fixture473def multiindex_year_month_day_dataframe_random_data():474"""475DataFrame with 3 level MultiIndex (year, month, day) covering476first 100 business days from 2000-01-01 with random data477"""478tdf = tm.makeTimeDataFrame(100)479ymd = tdf.groupby([lambda x: x.year, lambda x: x.month, lambda x: x.day]).sum()480# use Int64Index, to make sure things work481ymd.index = ymd.index.set_levels([lev.astype("i8") for lev in ymd.index.levels])482ymd.index.set_names(["year", "month", "day"], inplace=True)483return ymd484485486@pytest.fixture487def lexsorted_two_level_string_multiindex():488"""4892-level MultiIndex, lexsorted, with string names.490"""491return MultiIndex(492levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]],493codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],494names=["first", "second"],495)496497498@pytest.fixture499def multiindex_dataframe_random_data(lexsorted_two_level_string_multiindex):500"""DataFrame with 2 level MultiIndex with random data"""501index = lexsorted_two_level_string_multiindex502return DataFrame(503np.random.randn(10, 3), index=index, columns=Index(["A", "B", "C"], name="exp")504)505506507def _create_multiindex():508"""509MultiIndex used to test the general functionality of this object510"""511512# See Also: tests.multi.conftest.idx513major_axis = Index(["foo", "bar", "baz", "qux"])514minor_axis = Index(["one", "two"])515516major_codes = np.array([0, 0, 1, 2, 3, 3])517minor_codes = np.array([0, 1, 0, 1, 0, 1])518index_names = ["first", "second"]519return MultiIndex(520levels=[major_axis, minor_axis],521codes=[major_codes, minor_codes],522names=index_names,523verify_integrity=False,524)525526527def _create_mi_with_dt64tz_level():528"""529MultiIndex with a level that is a tzaware DatetimeIndex.530"""531# GH#8367 round trip with pickle532return MultiIndex.from_product(533[[1, 2], ["a", "b"], pd.date_range("20130101", periods=3, tz="US/Eastern")],534names=["one", "two", "three"],535)536537538indices_dict = {539"unicode": tm.makeUnicodeIndex(100),540"string": tm.makeStringIndex(100),541"datetime": tm.makeDateIndex(100),542"datetime-tz": tm.makeDateIndex(100, tz="US/Pacific"),543"period": tm.makePeriodIndex(100),544"timedelta": tm.makeTimedeltaIndex(100),545"int": tm.makeIntIndex(100),546"uint": tm.makeUIntIndex(100),547"range": tm.makeRangeIndex(100),548"float": tm.makeFloatIndex(100),549"num_int64": tm.makeNumericIndex(100, dtype="int64"),550"num_int32": tm.makeNumericIndex(100, dtype="int32"),551"num_int16": tm.makeNumericIndex(100, dtype="int16"),552"num_int8": tm.makeNumericIndex(100, dtype="int8"),553"num_uint64": tm.makeNumericIndex(100, dtype="uint64"),554"num_uint32": tm.makeNumericIndex(100, dtype="uint32"),555"num_uint16": tm.makeNumericIndex(100, dtype="uint16"),556"num_uint8": tm.makeNumericIndex(100, dtype="uint8"),557"num_float64": tm.makeNumericIndex(100, dtype="float64"),558"num_float32": tm.makeNumericIndex(100, dtype="float32"),559"bool": tm.makeBoolIndex(10),560"categorical": tm.makeCategoricalIndex(100),561"interval": tm.makeIntervalIndex(100),562"empty": Index([]),563"tuples": MultiIndex.from_tuples(zip(["foo", "bar", "baz"], [1, 2, 3])),564"mi-with-dt64tz-level": _create_mi_with_dt64tz_level(),565"multi": _create_multiindex(),566"repeats": Index([0, 0, 1, 1, 2, 2]),567"nullable_int": Index(np.arange(100), dtype="Int64"),568"nullable_uint": Index(np.arange(100), dtype="UInt16"),569"nullable_float": Index(np.arange(100), dtype="Float32"),570"nullable_bool": Index(np.arange(100).astype(bool), dtype="boolean"),571"string-python": Index(pd.array(tm.makeStringIndex(100), dtype="string[python]")),572}573if has_pyarrow:574idx = Index(pd.array(tm.makeStringIndex(100), dtype="string[pyarrow]"))575indices_dict["string-pyarrow"] = idx576577578@pytest.fixture(params=indices_dict.keys())579def index(request):580"""581Fixture for many "simple" kinds of indices.582583These indices are unlikely to cover corner cases, e.g.584- no names585- no NaTs/NaNs586- no values near implementation bounds587- ...588"""589# copy to avoid mutation, e.g. setting .name590return indices_dict[request.param].copy()591592593# Needed to generate cartesian product of indices594index_fixture2 = index595596597@pytest.fixture(598params=[599key for key in indices_dict if not isinstance(indices_dict[key], MultiIndex)600]601)602def index_flat(request):603"""604index fixture, but excluding MultiIndex cases.605"""606key = request.param607return indices_dict[key].copy()608609610# Alias so we can test with cartesian product of index_flat611index_flat2 = index_flat612613614@pytest.fixture(615params=[616key617for key in indices_dict618if not isinstance(indices_dict[key], MultiIndex) and indices_dict[key].is_unique619]620)621def index_flat_unique(request):622"""623index_flat with uniqueness requirement.624"""625key = request.param626return indices_dict[key].copy()627628629@pytest.fixture(630params=[631key632for key in indices_dict633if not (634key in ["int", "uint", "range", "empty", "repeats"]635or key.startswith("num_")636)637and not isinstance(indices_dict[key], MultiIndex)638]639)640def index_with_missing(request):641"""642Fixture for indices with missing values.643644Integer-dtype and empty cases are excluded because they cannot hold missing645values.646647MultiIndex is excluded because isna() is not defined for MultiIndex.648"""649650# GH 35538. Use deep copy to avoid illusive bug on np-dev651# Azure pipeline that writes into indices_dict despite copy652ind = indices_dict[request.param].copy(deep=True)653vals = ind.values654if request.param in ["tuples", "mi-with-dt64tz-level", "multi"]:655# For setting missing values in the top level of MultiIndex656vals = ind.tolist()657vals[0] = (None,) + vals[0][1:]658vals[-1] = (None,) + vals[-1][1:]659return MultiIndex.from_tuples(vals)660else:661vals[0] = None662vals[-1] = None663return type(ind)(vals)664665666# ----------------------------------------------------------------667# Series'668# ----------------------------------------------------------------669@pytest.fixture670def string_series():671"""672Fixture for Series of floats with Index of unique strings673"""674s = tm.makeStringSeries()675s.name = "series"676return s677678679@pytest.fixture680def object_series():681"""682Fixture for Series of dtype object with Index of unique strings683"""684s = tm.makeObjectSeries()685s.name = "objects"686return s687688689@pytest.fixture690def datetime_series():691"""692Fixture for Series of floats with DatetimeIndex693"""694s = tm.makeTimeSeries()695s.name = "ts"696return s697698699def _create_series(index):700"""Helper for the _series dict"""701size = len(index)702data = np.random.randn(size)703return Series(data, index=index, name="a")704705706_series = {707f"series-with-{index_id}-index": _create_series(index)708for index_id, index in indices_dict.items()709}710711712@pytest.fixture713def series_with_simple_index(index):714"""715Fixture for tests on series with changing types of indices.716"""717return _create_series(index)718719720@pytest.fixture721def series_with_multilevel_index():722"""723Fixture with a Series with a 2-level MultiIndex.724"""725arrays = [726["bar", "bar", "baz", "baz", "qux", "qux", "foo", "foo"],727["one", "two", "one", "two", "one", "two", "one", "two"],728]729tuples = zip(*arrays)730index = MultiIndex.from_tuples(tuples)731data = np.random.randn(8)732ser = Series(data, index=index)733ser[3] = np.NaN734return ser735736737_narrow_series = {738f"{dtype.__name__}-series": tm.makeFloatSeries(name="a").astype(dtype)739for dtype in tm.NARROW_NP_DTYPES740}741742743_index_or_series_objs = {**indices_dict, **_series, **_narrow_series}744745746@pytest.fixture(params=_index_or_series_objs.keys())747def index_or_series_obj(request):748"""749Fixture for tests on indexes, series and series with a narrow dtype750copy to avoid mutation, e.g. setting .name751"""752return _index_or_series_objs[request.param].copy(deep=True)753754755# ----------------------------------------------------------------756# DataFrames757# ----------------------------------------------------------------758@pytest.fixture759def int_frame():760"""761Fixture for DataFrame of ints with index of unique strings762763Columns are ['A', 'B', 'C', 'D']764765A B C D766vpBeWjM651 1 0 1 07675JyxmrP1En -1 0 0 0768qEDaoD49U2 -1 1 0 0769m66TkTfsFe 0 0 0 0770EHPaNzEUFm -1 0 -1 0771fpRJCevQhi 2 0 0 0772OlQvnmfi3Q 0 0 -2 0773... .. .. .. ..774uB1FPlz4uP 0 0 0 1775EcSe6yNzCU 0 0 -1 0776L50VudaiI8 -1 1 -2 0777y3bpw4nwIp 0 -1 0 0778H0RdLLwrCT 1 1 0 0779rY82K0vMwm 0 0 0 07801OPIUjnkjk 2 0 0 0781782[30 rows x 4 columns]783"""784return DataFrame(tm.getSeriesData()).astype("int64")785786787@pytest.fixture788def datetime_frame():789"""790Fixture for DataFrame of floats with DatetimeIndex791792Columns are ['A', 'B', 'C', 'D']793794A B C D7952000-01-03 -1.122153 0.468535 0.122226 1.6937117962000-01-04 0.189378 0.486100 0.007864 -1.2160527972000-01-05 0.041401 -0.835752 -0.035279 -0.4143577982000-01-06 0.430050 0.894352 0.090719 0.0369397992000-01-07 -0.620982 -0.668211 -0.706153 1.4663358002000-01-10 -0.752633 0.328434 -0.815325 0.6996748012000-01-11 -2.236969 0.615737 -0.829076 -1.196106802... ... ... ... ...8032000-02-03 1.642618 -0.579288 0.046005 1.3852498042000-02-04 -0.544873 -1.160962 -0.284071 -1.4183518052000-02-07 -2.656149 -0.601387 1.410148 0.4441508062000-02-08 -1.201881 -1.289040 0.772992 -1.4453008072000-02-09 1.377373 0.398619 1.008453 -0.9282078082000-02-10 0.473194 -0.636677 0.984058 0.5115198092000-02-11 -0.965556 0.408313 -1.312844 -0.381948810811[30 rows x 4 columns]812"""813return DataFrame(tm.getTimeSeriesData())814815816@pytest.fixture817def float_frame():818"""819Fixture for DataFrame of floats with index of unique strings820821Columns are ['A', 'B', 'C', 'D'].822823A B C D824P7GACiRnxd -0.465578 -0.361863 0.886172 -0.053465825qZKh6afn8n -0.466693 -0.373773 0.266873 1.673901826tkp0r6Qble 0.148691 -0.059051 0.174817 1.598433827wP70WOCtv8 0.133045 -0.581994 -0.992240 0.261651828M2AeYQMnCz -1.207959 -0.185775 0.588206 0.563938829QEPzyGDYDo -0.381843 -0.758281 0.502575 -0.565053830r78Jwns6dn -0.653707 0.883127 0.682199 0.206159831... ... ... ... ...832IHEGx9NO0T -0.277360 0.113021 -1.018314 0.196316833lPMj8K27FA -1.313667 -0.604776 -1.305618 -0.863999834qa66YMWQa5 1.110525 0.475310 -0.747865 0.032121835yOa0ATsmcE -0.431457 0.067094 0.096567 -0.26496283665znX3uRNG 1.528446 0.160416 -0.109635 -0.032987837eCOBvKqf3e 0.235281 1.622222 0.781255 0.392871838xSucinXxuV -1.263557 0.252799 -0.552247 0.400426839840[30 rows x 4 columns]841"""842return DataFrame(tm.getSeriesData())843844845@pytest.fixture846def mixed_type_frame():847"""848Fixture for DataFrame of float/int/string columns with RangeIndex849Columns are ['a', 'b', 'c', 'float32', 'int32'].850"""851return DataFrame(852{853"a": 1.0,854"b": 2,855"c": "foo",856"float32": np.array([1.0] * 10, dtype="float32"),857"int32": np.array([1] * 10, dtype="int32"),858},859index=np.arange(10),860)861862863@pytest.fixture864def rand_series_with_duplicate_datetimeindex():865"""866Fixture for Series with a DatetimeIndex that has duplicates.867"""868dates = [869datetime(2000, 1, 2),870datetime(2000, 1, 2),871datetime(2000, 1, 2),872datetime(2000, 1, 3),873datetime(2000, 1, 3),874datetime(2000, 1, 3),875datetime(2000, 1, 4),876datetime(2000, 1, 4),877datetime(2000, 1, 4),878datetime(2000, 1, 5),879]880881return Series(np.random.randn(len(dates)), index=dates)882883884# ----------------------------------------------------------------885# Scalars886# ----------------------------------------------------------------887@pytest.fixture(888params=[889(Interval(left=0, right=5), IntervalDtype("int64", "right")),890(Interval(left=0.1, right=0.5), IntervalDtype("float64", "right")),891(Period("2012-01", freq="M"), "period[M]"),892(Period("2012-02-01", freq="D"), "period[D]"),893(894Timestamp("2011-01-01", tz="US/Eastern"),895DatetimeTZDtype(tz="US/Eastern"),896),897(Timedelta(seconds=500), "timedelta64[ns]"),898]899)900def ea_scalar_and_dtype(request):901return request.param902903904# ----------------------------------------------------------------905# Operators & Operations906# ----------------------------------------------------------------907_all_arithmetic_operators = [908"__add__",909"__radd__",910"__sub__",911"__rsub__",912"__mul__",913"__rmul__",914"__floordiv__",915"__rfloordiv__",916"__truediv__",917"__rtruediv__",918"__pow__",919"__rpow__",920"__mod__",921"__rmod__",922]923924925@pytest.fixture(params=_all_arithmetic_operators)926def all_arithmetic_operators(request):927"""928Fixture for dunder names for common arithmetic operations.929"""930return request.param931932933@pytest.fixture(934params=[935operator.add,936ops.radd,937operator.sub,938ops.rsub,939operator.mul,940ops.rmul,941operator.truediv,942ops.rtruediv,943operator.floordiv,944ops.rfloordiv,945operator.mod,946ops.rmod,947operator.pow,948ops.rpow,949operator.eq,950operator.ne,951operator.lt,952operator.le,953operator.gt,954operator.ge,955operator.and_,956ops.rand_,957operator.xor,958ops.rxor,959operator.or_,960ops.ror_,961]962)963def all_binary_operators(request):964"""965Fixture for operator and roperator arithmetic, comparison, and logical ops.966"""967return request.param968969970@pytest.fixture(971params=[972operator.add,973ops.radd,974operator.sub,975ops.rsub,976operator.mul,977ops.rmul,978operator.truediv,979ops.rtruediv,980operator.floordiv,981ops.rfloordiv,982operator.mod,983ops.rmod,984operator.pow,985ops.rpow,986]987)988def all_arithmetic_functions(request):989"""990Fixture for operator and roperator arithmetic functions.991992Notes993-----994This includes divmod and rdivmod, whereas all_arithmetic_operators995does not.996"""997return request.param9989991000_all_numeric_reductions = [1001"sum",1002"max",1003"min",1004"mean",1005"prod",1006"std",1007"var",1008"median",1009"kurt",1010"skew",1011]101210131014@pytest.fixture(params=_all_numeric_reductions)1015def all_numeric_reductions(request):1016"""1017Fixture for numeric reduction names.1018"""1019return request.param102010211022_all_boolean_reductions = ["all", "any"]102310241025@pytest.fixture(params=_all_boolean_reductions)1026def all_boolean_reductions(request):1027"""1028Fixture for boolean reduction names.1029"""1030return request.param103110321033_all_reductions = _all_numeric_reductions + _all_boolean_reductions103410351036@pytest.fixture(params=_all_reductions)1037def all_reductions(request):1038"""1039Fixture for all (boolean + numeric) reduction names.1040"""1041return request.param104210431044@pytest.fixture(1045params=[1046operator.eq,1047operator.ne,1048operator.gt,1049operator.ge,1050operator.lt,1051operator.le,1052]1053)1054def comparison_op(request):1055"""1056Fixture for operator module comparison functions.1057"""1058return request.param105910601061@pytest.fixture(params=["__le__", "__lt__", "__ge__", "__gt__"])1062def compare_operators_no_eq_ne(request):1063"""1064Fixture for dunder names for compare operations except == and !=10651066* >=1067* >1068* <1069* <=1070"""1071return request.param107210731074@pytest.fixture(1075params=["__and__", "__rand__", "__or__", "__ror__", "__xor__", "__rxor__"]1076)1077def all_logical_operators(request):1078"""1079Fixture for dunder names for common logical operations10801081* |1082* &1083* ^1084"""1085return request.param108610871088# ----------------------------------------------------------------1089# Data sets/files1090# ----------------------------------------------------------------1091@pytest.fixture1092def strict_data_files(pytestconfig):1093"""1094Returns the configuration for the test setting `--strict-data-files`.1095"""1096return pytestconfig.getoption("--strict-data-files")109710981099@pytest.fixture1100def datapath(strict_data_files):1101"""1102Get the path to a data file.11031104Parameters1105----------1106path : str1107Path to the file, relative to ``pandas/tests/``11081109Returns1110-------1111path including ``pandas/tests``.11121113Raises1114------1115ValueError1116If the path doesn't exist and the --strict-data-files option is set.1117"""1118BASE_PATH = os.path.join(os.path.dirname(__file__), "tests")11191120def deco(*args):1121path = os.path.join(BASE_PATH, *args)1122if not os.path.exists(path):1123if strict_data_files:1124raise ValueError(1125f"Could not find file {path} and --strict-data-files is set."1126)1127else:1128pytest.skip(f"Could not find {path}.")1129return path11301131return deco113211331134@pytest.fixture1135def iris(datapath):1136"""1137The iris dataset as a DataFrame.1138"""1139return pd.read_csv(datapath("io", "data", "csv", "iris.csv"))114011411142# ----------------------------------------------------------------1143# Time zones1144# ----------------------------------------------------------------1145TIMEZONES = [1146None,1147"UTC",1148"US/Eastern",1149"Asia/Tokyo",1150"dateutil/US/Pacific",1151"dateutil/Asia/Singapore",1152"+01:15",1153"-02:15",1154"UTC+01:15",1155"UTC-02:15",1156tzutc(),1157tzlocal(),1158FixedOffset(300),1159FixedOffset(0),1160FixedOffset(-300),1161timezone.utc,1162timezone(timedelta(hours=1)),1163timezone(timedelta(hours=-1), name="foo"),1164]1165TIMEZONE_IDS = [repr(i) for i in TIMEZONES]116611671168@td.parametrize_fixture_doc(str(TIMEZONE_IDS))1169@pytest.fixture(params=TIMEZONES, ids=TIMEZONE_IDS)1170def tz_naive_fixture(request):1171"""1172Fixture for trying timezones including default (None): {0}1173"""1174return request.param117511761177@td.parametrize_fixture_doc(str(TIMEZONE_IDS[1:]))1178@pytest.fixture(params=TIMEZONES[1:], ids=TIMEZONE_IDS[1:])1179def tz_aware_fixture(request):1180"""1181Fixture for trying explicit timezones: {0}1182"""1183return request.param118411851186# Generate cartesian product of tz_aware_fixture:1187tz_aware_fixture2 = tz_aware_fixture118811891190@pytest.fixture(params=["utc", "dateutil/UTC", utc, tzutc(), timezone.utc])1191def utc_fixture(request):1192"""1193Fixture to provide variants of UTC timezone strings and tzinfo objects.1194"""1195return request.param119611971198utc_fixture2 = utc_fixture119912001201# ----------------------------------------------------------------1202# Dtypes1203# ----------------------------------------------------------------1204@pytest.fixture(params=tm.STRING_DTYPES)1205def string_dtype(request):1206"""1207Parametrized fixture for string dtypes.12081209* str1210* 'str'1211* 'U'1212"""1213return request.param121412151216@pytest.fixture(1217params=[1218"string[python]",1219pytest.param(1220"string[pyarrow]", marks=td.skip_if_no("pyarrow", min_version="1.0.0")1221),1222]1223)1224def nullable_string_dtype(request):1225"""1226Parametrized fixture for string dtypes.12271228* 'string[python]'1229* 'string[pyarrow]'1230"""1231return request.param123212331234@pytest.fixture(1235params=[1236"python",1237pytest.param("pyarrow", marks=td.skip_if_no("pyarrow", min_version="1.0.0")),1238]1239)1240def string_storage(request):1241"""1242Parametrized fixture for pd.options.mode.string_storage.12431244* 'python'1245* 'pyarrow'1246"""1247return request.param124812491250# Alias so we can test with cartesian product of string_storage1251string_storage2 = string_storage125212531254@pytest.fixture(params=tm.BYTES_DTYPES)1255def bytes_dtype(request):1256"""1257Parametrized fixture for bytes dtypes.12581259* bytes1260* 'bytes'1261"""1262return request.param126312641265@pytest.fixture(params=tm.OBJECT_DTYPES)1266def object_dtype(request):1267"""1268Parametrized fixture for object dtypes.12691270* object1271* 'object'1272"""1273return request.param127412751276@pytest.fixture(1277params=[1278"object",1279"string[python]",1280pytest.param(1281"string[pyarrow]", marks=td.skip_if_no("pyarrow", min_version="1.0.0")1282),1283]1284)1285def any_string_dtype(request):1286"""1287Parametrized fixture for string dtypes.1288* 'object'1289* 'string[python]'1290* 'string[pyarrow]'1291"""1292return request.param129312941295@pytest.fixture(params=tm.DATETIME64_DTYPES)1296def datetime64_dtype(request):1297"""1298Parametrized fixture for datetime64 dtypes.12991300* 'datetime64[ns]'1301* 'M8[ns]'1302"""1303return request.param130413051306@pytest.fixture(params=tm.TIMEDELTA64_DTYPES)1307def timedelta64_dtype(request):1308"""1309Parametrized fixture for timedelta64 dtypes.13101311* 'timedelta64[ns]'1312* 'm8[ns]'1313"""1314return request.param131513161317@pytest.fixture1318def fixed_now_ts():1319"""1320Fixture emits fixed Timestamp.now()1321"""1322return Timestamp(1323year=2021, month=1, day=1, hour=12, minute=4, second=13, microsecond=221324)132513261327@pytest.fixture(params=tm.FLOAT_NUMPY_DTYPES)1328def float_numpy_dtype(request):1329"""1330Parameterized fixture for float dtypes.13311332* float1333* 'float32'1334* 'float64'1335"""1336return request.param133713381339@pytest.fixture(params=tm.FLOAT_EA_DTYPES)1340def float_ea_dtype(request):1341"""1342Parameterized fixture for float dtypes.13431344* 'Float32'1345* 'Float64'1346"""1347return request.param134813491350@pytest.fixture(params=tm.FLOAT_NUMPY_DTYPES + tm.FLOAT_EA_DTYPES)1351def any_float_dtype(request):1352"""1353Parameterized fixture for float dtypes.13541355* float1356* 'float32'1357* 'float64'1358* 'Float32'1359* 'Float64'1360"""1361return request.param136213631364@pytest.fixture(params=tm.COMPLEX_DTYPES)1365def complex_dtype(request):1366"""1367Parameterized fixture for complex dtypes.13681369* complex1370* 'complex64'1371* 'complex128'1372"""1373return request.param137413751376@pytest.fixture(params=tm.SIGNED_INT_NUMPY_DTYPES)1377def any_signed_int_numpy_dtype(request):1378"""1379Parameterized fixture for signed integer dtypes.13801381* int1382* 'int8'1383* 'int16'1384* 'int32'1385* 'int64'1386"""1387return request.param138813891390@pytest.fixture(params=tm.UNSIGNED_INT_NUMPY_DTYPES)1391def any_unsigned_int_numpy_dtype(request):1392"""1393Parameterized fixture for unsigned integer dtypes.13941395* 'uint8'1396* 'uint16'1397* 'uint32'1398* 'uint64'1399"""1400return request.param140114021403@pytest.fixture(params=tm.ALL_INT_NUMPY_DTYPES)1404def any_int_numpy_dtype(request):1405"""1406Parameterized fixture for any integer dtype.14071408* int1409* 'int8'1410* 'uint8'1411* 'int16'1412* 'uint16'1413* 'int32'1414* 'uint32'1415* 'int64'1416* 'uint64'1417"""1418return request.param141914201421@pytest.fixture(params=tm.ALL_INT_EA_DTYPES)1422def any_int_ea_dtype(request):1423"""1424Parameterized fixture for any nullable integer dtype.14251426* 'UInt8'1427* 'Int8'1428* 'UInt16'1429* 'Int16'1430* 'UInt32'1431* 'Int32'1432* 'UInt64'1433* 'Int64'1434"""1435return request.param143614371438@pytest.fixture(params=tm.ALL_INT_NUMPY_DTYPES + tm.ALL_INT_EA_DTYPES)1439def any_int_dtype(request):1440"""1441Parameterized fixture for any nullable integer dtype.14421443* int1444* 'int8'1445* 'uint8'1446* 'int16'1447* 'uint16'1448* 'int32'1449* 'uint32'1450* 'int64'1451* 'uint64'1452* 'UInt8'1453* 'Int8'1454* 'UInt16'1455* 'Int16'1456* 'UInt32'1457* 'Int32'1458* 'UInt64'1459* 'Int64'1460"""1461return request.param146214631464@pytest.fixture(params=tm.ALL_INT_EA_DTYPES + tm.FLOAT_EA_DTYPES)1465def any_numeric_ea_dtype(request):1466"""1467Parameterized fixture for any nullable integer dtype and1468any float ea dtypes.14691470* 'UInt8'1471* 'Int8'1472* 'UInt16'1473* 'Int16'1474* 'UInt32'1475* 'Int32'1476* 'UInt64'1477* 'Int64'1478* 'Float32'1479* 'Float64'1480"""1481return request.param148214831484@pytest.fixture(params=tm.SIGNED_INT_EA_DTYPES)1485def any_signed_int_ea_dtype(request):1486"""1487Parameterized fixture for any signed nullable integer dtype.14881489* 'Int8'1490* 'Int16'1491* 'Int32'1492* 'Int64'1493"""1494return request.param149514961497@pytest.fixture(params=tm.ALL_REAL_NUMPY_DTYPES)1498def any_real_numpy_dtype(request):1499"""1500Parameterized fixture for any (purely) real numeric dtype.15011502* int1503* 'int8'1504* 'uint8'1505* 'int16'1506* 'uint16'1507* 'int32'1508* 'uint32'1509* 'int64'1510* 'uint64'1511* float1512* 'float32'1513* 'float64'1514"""1515return request.param151615171518@pytest.fixture(params=tm.ALL_NUMPY_DTYPES)1519def any_numpy_dtype(request):1520"""1521Parameterized fixture for all numpy dtypes.15221523* bool1524* 'bool'1525* int1526* 'int8'1527* 'uint8'1528* 'int16'1529* 'uint16'1530* 'int32'1531* 'uint32'1532* 'int64'1533* 'uint64'1534* float1535* 'float32'1536* 'float64'1537* complex1538* 'complex64'1539* 'complex128'1540* str1541* 'str'1542* 'U'1543* bytes1544* 'bytes'1545* 'datetime64[ns]'1546* 'M8[ns]'1547* 'timedelta64[ns]'1548* 'm8[ns]'1549* object1550* 'object'1551"""1552return request.param155315541555# categoricals are handled separately1556_any_skipna_inferred_dtype = [1557("string", ["a", np.nan, "c"]),1558("string", ["a", pd.NA, "c"]),1559("bytes", [b"a", np.nan, b"c"]),1560("empty", [np.nan, np.nan, np.nan]),1561("empty", []),1562("mixed-integer", ["a", np.nan, 2]),1563("mixed", ["a", np.nan, 2.0]),1564("floating", [1.0, np.nan, 2.0]),1565("integer", [1, np.nan, 2]),1566("mixed-integer-float", [1, np.nan, 2.0]),1567("decimal", [Decimal(1), np.nan, Decimal(2)]),1568("boolean", [True, np.nan, False]),1569("boolean", [True, pd.NA, False]),1570("datetime64", [np.datetime64("2013-01-01"), np.nan, np.datetime64("2018-01-01")]),1571("datetime", [Timestamp("20130101"), np.nan, Timestamp("20180101")]),1572("date", [date(2013, 1, 1), np.nan, date(2018, 1, 1)]),1573# The following two dtypes are commented out due to GH 235541574# ('complex', [1 + 1j, np.nan, 2 + 2j]),1575# ('timedelta64', [np.timedelta64(1, 'D'),1576# np.nan, np.timedelta64(2, 'D')]),1577("timedelta", [timedelta(1), np.nan, timedelta(2)]),1578("time", [time(1), np.nan, time(2)]),1579("period", [Period(2013), pd.NaT, Period(2018)]),1580("interval", [Interval(0, 1), np.nan, Interval(0, 2)]),1581]1582ids, _ = zip(*_any_skipna_inferred_dtype) # use inferred type as fixture-id158315841585@pytest.fixture(params=_any_skipna_inferred_dtype, ids=ids)1586def any_skipna_inferred_dtype(request):1587"""1588Fixture for all inferred dtypes from _libs.lib.infer_dtype15891590The covered (inferred) types are:1591* 'string'1592* 'empty'1593* 'bytes'1594* 'mixed'1595* 'mixed-integer'1596* 'mixed-integer-float'1597* 'floating'1598* 'integer'1599* 'decimal'1600* 'boolean'1601* 'datetime64'1602* 'datetime'1603* 'date'1604* 'timedelta'1605* 'time'1606* 'period'1607* 'interval'16081609Returns1610-------1611inferred_dtype : str1612The string for the inferred dtype from _libs.lib.infer_dtype1613values : np.ndarray1614An array of object dtype that will be inferred to have1615`inferred_dtype`16161617Examples1618--------1619>>> import pandas._libs.lib as lib1620>>>1621>>> def test_something(any_skipna_inferred_dtype):1622... inferred_dtype, values = any_skipna_inferred_dtype1623... # will pass1624... assert lib.infer_dtype(values, skipna=True) == inferred_dtype1625"""1626inferred_dtype, values = request.param1627values = np.array(values, dtype=object) # object dtype to avoid casting16281629# correctness of inference tested in tests/dtypes/test_inference.py1630return inferred_dtype, values163116321633# ----------------------------------------------------------------1634# Misc1635# ----------------------------------------------------------------1636@pytest.fixture1637def ip():1638"""1639Get an instance of IPython.InteractiveShell.16401641Will raise a skip if IPython is not installed.1642"""1643pytest.importorskip("IPython", minversion="6.0.0")1644from IPython.core.interactiveshell import InteractiveShell16451646# GH#35711 make sure sqlite history file handle is not leaked1647from traitlets.config import Config # isort:skip16481649c = Config()1650c.HistoryManager.hist_file = ":memory:"16511652return InteractiveShell(config=c)165316541655@pytest.fixture(params=["bsr", "coo", "csc", "csr", "dia", "dok", "lil"])1656def spmatrix(request):1657"""1658Yields scipy sparse matrix classes.1659"""1660from scipy import sparse16611662return getattr(sparse, request.param + "_matrix")166316641665@pytest.fixture(1666params=[1667getattr(pd.offsets, o)1668for o in pd.offsets.__all__1669if issubclass(getattr(pd.offsets, o), pd.offsets.Tick)1670]1671)1672def tick_classes(request):1673"""1674Fixture for Tick based datetime offsets available for a time series.1675"""1676return request.param167716781679@pytest.fixture(params=[None, lambda x: x])1680def sort_by_key(request):1681"""1682Simple fixture for testing keys in sorting methods.1683Tests None (no key) and the identity key.1684"""1685return request.param168616871688@pytest.fixture()1689def fsspectest():1690pytest.importorskip("fsspec")1691from fsspec import register_implementation1692from fsspec.implementations.memory import MemoryFileSystem1693from fsspec.registry import _registry as registry16941695class TestMemoryFS(MemoryFileSystem):1696protocol = "testmem"1697test = [None]16981699def __init__(self, **kwargs):1700self.test[0] = kwargs.pop("test", None)1701super().__init__(**kwargs)17021703register_implementation("testmem", TestMemoryFS, clobber=True)1704yield TestMemoryFS()1705registry.pop("testmem", None)1706TestMemoryFS.test[0] = None1707TestMemoryFS.store.clear()170817091710@pytest.fixture(1711params=[1712("foo", None, None),1713("Egon", "Venkman", None),1714("NCC1701D", "NCC1701D", "NCC1701D"),1715# possibly-matching NAs1716(np.nan, np.nan, np.nan),1717(np.nan, pd.NaT, None),1718(np.nan, pd.NA, None),1719(pd.NA, pd.NA, pd.NA),1720]1721)1722def names(request):1723"""1724A 3-tuple of names, the first two for operands, the last for a result.1725"""1726return request.param172717281729@pytest.fixture(params=[tm.setitem, tm.loc, tm.iloc])1730def indexer_sli(request):1731"""1732Parametrize over __setitem__, loc.__setitem__, iloc.__setitem__1733"""1734return request.param173517361737@pytest.fixture(params=[tm.setitem, tm.iloc])1738def indexer_si(request):1739"""1740Parametrize over __setitem__, iloc.__setitem__1741"""1742return request.param174317441745@pytest.fixture(params=[tm.setitem, tm.loc])1746def indexer_sl(request):1747"""1748Parametrize over __setitem__, loc.__setitem__1749"""1750return request.param175117521753@pytest.fixture(params=[tm.at, tm.loc])1754def indexer_al(request):1755"""1756Parametrize over at.__setitem__, loc.__setitem__1757"""1758return request.param175917601761@pytest.fixture(params=[tm.iat, tm.iloc])1762def indexer_ial(request):1763"""1764Parametrize over iat.__setitem__, iloc.__setitem__1765"""1766return request.param176717681769@pytest.fixture1770def using_array_manager(request):1771"""1772Fixture to check if the array manager is being used.1773"""1774return pd.options.mode.data_manager == "array"177517761777