conftest.py 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656
  1. """
  2. This file is very long and growing, but it was decided to not split it yet, as
  3. it's still manageable (2020-03-17, ~1.1k LoC). See gh-31989
  4. Instead of splitting it was decided to define sections here:
  5. - Configuration / Settings
  6. - Autouse fixtures
  7. - Common arguments
  8. - Missing values & co.
  9. - Classes
  10. - Indices
  11. - Series'
  12. - DataFrames
  13. - Operators & Operations
  14. - Data sets/files
  15. - Time zones
  16. - Dtypes
  17. - Misc
  18. """
  19. from collections import abc
  20. from datetime import (
  21. date,
  22. datetime,
  23. time,
  24. timedelta,
  25. timezone,
  26. )
  27. from decimal import Decimal
  28. import operator
  29. import os
  30. from dateutil.tz import (
  31. tzlocal,
  32. tzutc,
  33. )
  34. import hypothesis
  35. from hypothesis import strategies as st
  36. import numpy as np
  37. import pytest
  38. from pytz import (
  39. FixedOffset,
  40. utc,
  41. )
  42. import pandas.util._test_decorators as td
  43. from pandas.core.dtypes.dtypes import (
  44. DatetimeTZDtype,
  45. IntervalDtype,
  46. )
  47. import pandas as pd
  48. from pandas import (
  49. DataFrame,
  50. Interval,
  51. Period,
  52. Series,
  53. Timedelta,
  54. Timestamp,
  55. )
  56. import pandas._testing as tm
  57. from pandas.core import ops
  58. from pandas.core.indexes.api import (
  59. Index,
  60. MultiIndex,
  61. )
  62. # Until https://github.com/numpy/numpy/issues/19078 is sorted out, just suppress
  63. suppress_npdev_promotion_warning = pytest.mark.filterwarnings(
  64. "ignore:Promotion of numbers and bools:FutureWarning"
  65. )
  66. # ----------------------------------------------------------------
  67. # Configuration / Settings
  68. # ----------------------------------------------------------------
  69. # pytest
  70. def pytest_addoption(parser):
  71. parser.addoption("--skip-slow", action="store_true", help="skip slow tests")
  72. parser.addoption("--skip-network", action="store_true", help="skip network tests")
  73. parser.addoption("--skip-db", action="store_true", help="skip db tests")
  74. parser.addoption(
  75. "--run-high-memory", action="store_true", help="run high memory tests"
  76. )
  77. parser.addoption("--only-slow", action="store_true", help="run only slow tests")
  78. parser.addoption(
  79. "--strict-data-files",
  80. action="store_true",
  81. help="Fail if a test is skipped for missing data file.",
  82. )
  83. def pytest_runtest_setup(item):
  84. if "slow" in item.keywords and item.config.getoption("--skip-slow"):
  85. pytest.skip("skipping due to --skip-slow")
  86. if "slow" not in item.keywords and item.config.getoption("--only-slow"):
  87. pytest.skip("skipping due to --only-slow")
  88. if "network" in item.keywords and item.config.getoption("--skip-network"):
  89. pytest.skip("skipping due to --skip-network")
  90. if "db" in item.keywords and item.config.getoption("--skip-db"):
  91. pytest.skip("skipping due to --skip-db")
  92. if "high_memory" in item.keywords and not item.config.getoption(
  93. "--run-high-memory"
  94. ):
  95. pytest.skip("skipping high memory test since --run-high-memory was not set")
  96. def pytest_collection_modifyitems(items):
  97. for item in items:
  98. # mark all tests in the pandas/tests/frame directory with "arraymanager"
  99. if "/frame/" in item.nodeid:
  100. item.add_marker(pytest.mark.arraymanager)
  101. item.add_marker(suppress_npdev_promotion_warning)
  102. # Hypothesis
  103. hypothesis.settings.register_profile(
  104. "ci",
  105. # Hypothesis timing checks are tuned for scalars by default, so we bump
  106. # them from 200ms to 500ms per test case as the global default. If this
  107. # is too short for a specific test, (a) try to make it faster, and (b)
  108. # if it really is slow add `@settings(deadline=...)` with a working value,
  109. # or `deadline=None` to entirely disable timeouts for that test.
  110. deadline=500,
  111. suppress_health_check=(hypothesis.HealthCheck.too_slow,),
  112. )
  113. hypothesis.settings.load_profile("ci")
  114. # Registering these strategies makes them globally available via st.from_type,
  115. # which is use for offsets in tests/tseries/offsets/test_offsets_properties.py
  116. for name in "MonthBegin MonthEnd BMonthBegin BMonthEnd".split():
  117. cls = getattr(pd.tseries.offsets, name)
  118. st.register_type_strategy(
  119. cls, st.builds(cls, n=st.integers(-99, 99), normalize=st.booleans())
  120. )
  121. for name in "YearBegin YearEnd BYearBegin BYearEnd".split():
  122. cls = getattr(pd.tseries.offsets, name)
  123. st.register_type_strategy(
  124. cls,
  125. st.builds(
  126. cls,
  127. n=st.integers(-5, 5),
  128. normalize=st.booleans(),
  129. month=st.integers(min_value=1, max_value=12),
  130. ),
  131. )
  132. for name in "QuarterBegin QuarterEnd BQuarterBegin BQuarterEnd".split():
  133. cls = getattr(pd.tseries.offsets, name)
  134. st.register_type_strategy(
  135. cls,
  136. st.builds(
  137. cls,
  138. n=st.integers(-24, 24),
  139. normalize=st.booleans(),
  140. startingMonth=st.integers(min_value=1, max_value=12),
  141. ),
  142. )
  143. # ----------------------------------------------------------------
  144. # Autouse fixtures
  145. # ----------------------------------------------------------------
  146. @pytest.fixture(autouse=True)
  147. def configure_tests():
  148. """
  149. Configure settings for all tests and test modules.
  150. """
  151. pd.set_option("chained_assignment", "raise")
  152. @pytest.fixture(autouse=True)
  153. def add_imports(doctest_namespace):
  154. """
  155. Make `np` and `pd` names available for doctests.
  156. """
  157. doctest_namespace["np"] = np
  158. doctest_namespace["pd"] = pd
  159. # ----------------------------------------------------------------
  160. # Common arguments
  161. # ----------------------------------------------------------------
  162. @pytest.fixture(params=[0, 1, "index", "columns"], ids=lambda x: f"axis={repr(x)}")
  163. def axis(request):
  164. """
  165. Fixture for returning the axis numbers of a DataFrame.
  166. """
  167. return request.param
  168. axis_frame = axis
  169. @pytest.fixture(params=[True, False, None])
  170. def observed(request):
  171. """
  172. Pass in the observed keyword to groupby for [True, False]
  173. This indicates whether categoricals should return values for
  174. values which are not in the grouper [False / None], or only values which
  175. appear in the grouper [True]. [None] is supported for future compatibility
  176. if we decide to change the default (and would need to warn if this
  177. parameter is not passed).
  178. """
  179. return request.param
  180. @pytest.fixture(params=[True, False, None])
  181. def ordered(request):
  182. """
  183. Boolean 'ordered' parameter for Categorical.
  184. """
  185. return request.param
  186. @pytest.fixture(params=["first", "last", False])
  187. def keep(request):
  188. """
  189. Valid values for the 'keep' parameter used in
  190. .duplicated or .drop_duplicates
  191. """
  192. return request.param
  193. @pytest.fixture(params=["left", "right", "both", "neither"])
  194. def closed(request):
  195. """
  196. Fixture for trying all interval closed parameters.
  197. """
  198. return request.param
  199. @pytest.fixture(params=["left", "right", "both", "neither"])
  200. def other_closed(request):
  201. """
  202. Secondary closed fixture to allow parametrizing over all pairs of closed.
  203. """
  204. return request.param
  205. @pytest.fixture(params=[None, "gzip", "bz2", "zip", "xz"])
  206. def compression(request):
  207. """
  208. Fixture for trying common compression types in compression tests.
  209. """
  210. return request.param
  211. @pytest.fixture(params=["gzip", "bz2", "zip", "xz"])
  212. def compression_only(request):
  213. """
  214. Fixture for trying common compression types in compression tests excluding
  215. uncompressed case.
  216. """
  217. return request.param
  218. @pytest.fixture(params=[True, False])
  219. def writable(request):
  220. """
  221. Fixture that an array is writable.
  222. """
  223. return request.param
  224. @pytest.fixture(params=["inner", "outer", "left", "right"])
  225. def join_type(request):
  226. """
  227. Fixture for trying all types of join operations.
  228. """
  229. return request.param
  230. @pytest.fixture(params=["nlargest", "nsmallest"])
  231. def nselect_method(request):
  232. """
  233. Fixture for trying all nselect methods.
  234. """
  235. return request.param
  236. # ----------------------------------------------------------------
  237. # Missing values & co.
  238. # ----------------------------------------------------------------
  239. @pytest.fixture(params=tm.NULL_OBJECTS, ids=lambda x: type(x).__name__)
  240. def nulls_fixture(request):
  241. """
  242. Fixture for each null type in pandas.
  243. """
  244. return request.param
  245. nulls_fixture2 = nulls_fixture # Generate cartesian product of nulls_fixture
  246. @pytest.fixture(params=[None, np.nan, pd.NaT])
  247. def unique_nulls_fixture(request):
  248. """
  249. Fixture for each null type in pandas, each null type exactly once.
  250. """
  251. return request.param
  252. # Generate cartesian product of unique_nulls_fixture:
  253. unique_nulls_fixture2 = unique_nulls_fixture
  254. # ----------------------------------------------------------------
  255. # Classes
  256. # ----------------------------------------------------------------
  257. @pytest.fixture(params=[DataFrame, Series])
  258. def frame_or_series(request):
  259. """
  260. Fixture to parametrize over DataFrame and Series.
  261. """
  262. return request.param
  263. # error: List item 0 has incompatible type "Type[Index]"; expected "Type[IndexOpsMixin]"
  264. @pytest.fixture(
  265. params=[Index, Series], ids=["index", "series"] # type: ignore[list-item]
  266. )
  267. def index_or_series(request):
  268. """
  269. Fixture to parametrize over Index and Series, made necessary by a mypy
  270. bug, giving an error:
  271. List item 0 has incompatible type "Type[Series]"; expected "Type[PandasObject]"
  272. See GH#29725
  273. """
  274. return request.param
  275. # Generate cartesian product of index_or_series fixture:
  276. index_or_series2 = index_or_series
  277. @pytest.fixture(params=[Index, Series, pd.array], ids=["index", "series", "array"])
  278. def index_or_series_or_array(request):
  279. """
  280. Fixture to parametrize over Index, Series, and ExtensionArray
  281. """
  282. return request.param
  283. @pytest.fixture
  284. def dict_subclass():
  285. """
  286. Fixture for a dictionary subclass.
  287. """
  288. class TestSubDict(dict):
  289. def __init__(self, *args, **kwargs):
  290. dict.__init__(self, *args, **kwargs)
  291. return TestSubDict
  292. @pytest.fixture
  293. def non_dict_mapping_subclass():
  294. """
  295. Fixture for a non-mapping dictionary subclass.
  296. """
  297. class TestNonDictMapping(abc.Mapping):
  298. def __init__(self, underlying_dict):
  299. self._data = underlying_dict
  300. def __getitem__(self, key):
  301. return self._data.__getitem__(key)
  302. def __iter__(self):
  303. return self._data.__iter__()
  304. def __len__(self):
  305. return self._data.__len__()
  306. return TestNonDictMapping
  307. # ----------------------------------------------------------------
  308. # Indices
  309. # ----------------------------------------------------------------
  310. @pytest.fixture
  311. def multiindex_year_month_day_dataframe_random_data():
  312. """
  313. DataFrame with 3 level MultiIndex (year, month, day) covering
  314. first 100 business days from 2000-01-01 with random data
  315. """
  316. tdf = tm.makeTimeDataFrame(100)
  317. ymd = tdf.groupby([lambda x: x.year, lambda x: x.month, lambda x: x.day]).sum()
  318. # use Int64Index, to make sure things work
  319. ymd.index = ymd.index.set_levels([lev.astype("i8") for lev in ymd.index.levels])
  320. ymd.index.set_names(["year", "month", "day"], inplace=True)
  321. return ymd
  322. @pytest.fixture
  323. def multiindex_dataframe_random_data():
  324. """DataFrame with 2 level MultiIndex with random data"""
  325. index = MultiIndex(
  326. levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]],
  327. codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
  328. names=["first", "second"],
  329. )
  330. return DataFrame(
  331. np.random.randn(10, 3), index=index, columns=Index(["A", "B", "C"], name="exp")
  332. )
  333. def _create_multiindex():
  334. """
  335. MultiIndex used to test the general functionality of this object
  336. """
  337. # See Also: tests.multi.conftest.idx
  338. major_axis = Index(["foo", "bar", "baz", "qux"])
  339. minor_axis = Index(["one", "two"])
  340. major_codes = np.array([0, 0, 1, 2, 3, 3])
  341. minor_codes = np.array([0, 1, 0, 1, 0, 1])
  342. index_names = ["first", "second"]
  343. return MultiIndex(
  344. levels=[major_axis, minor_axis],
  345. codes=[major_codes, minor_codes],
  346. names=index_names,
  347. verify_integrity=False,
  348. )
  349. def _create_mi_with_dt64tz_level():
  350. """
  351. MultiIndex with a level that is a tzaware DatetimeIndex.
  352. """
  353. # GH#8367 round trip with pickle
  354. return MultiIndex.from_product(
  355. [[1, 2], ["a", "b"], pd.date_range("20130101", periods=3, tz="US/Eastern")],
  356. names=["one", "two", "three"],
  357. )
  358. indices_dict = {
  359. "unicode": tm.makeUnicodeIndex(100),
  360. "string": tm.makeStringIndex(100),
  361. "datetime": tm.makeDateIndex(100),
  362. "datetime-tz": tm.makeDateIndex(100, tz="US/Pacific"),
  363. "period": tm.makePeriodIndex(100),
  364. "timedelta": tm.makeTimedeltaIndex(100),
  365. "int": tm.makeIntIndex(100),
  366. "uint": tm.makeUIntIndex(100),
  367. "range": tm.makeRangeIndex(100),
  368. "float": tm.makeFloatIndex(100),
  369. "bool": tm.makeBoolIndex(10),
  370. "categorical": tm.makeCategoricalIndex(100),
  371. "interval": tm.makeIntervalIndex(100),
  372. "empty": Index([]),
  373. "tuples": MultiIndex.from_tuples(zip(["foo", "bar", "baz"], [1, 2, 3])),
  374. "mi-with-dt64tz-level": _create_mi_with_dt64tz_level(),
  375. "multi": _create_multiindex(),
  376. "repeats": Index([0, 0, 1, 1, 2, 2]),
  377. }
  378. @pytest.fixture(params=indices_dict.keys())
  379. def index(request):
  380. """
  381. Fixture for many "simple" kinds of indices.
  382. These indices are unlikely to cover corner cases, e.g.
  383. - no names
  384. - no NaTs/NaNs
  385. - no values near implementation bounds
  386. - ...
  387. """
  388. # copy to avoid mutation, e.g. setting .name
  389. return indices_dict[request.param].copy()
  390. # Needed to generate cartesian product of indices
  391. index_fixture2 = index
  392. @pytest.fixture(
  393. params=[
  394. key for key in indices_dict if not isinstance(indices_dict[key], MultiIndex)
  395. ]
  396. )
  397. def index_flat(request):
  398. """
  399. index fixture, but excluding MultiIndex cases.
  400. """
  401. key = request.param
  402. return indices_dict[key].copy()
  403. # Alias so we can test with cartesian product of index_flat
  404. index_flat2 = index_flat
  405. @pytest.fixture(
  406. params=[
  407. key
  408. for key in indices_dict
  409. if key not in ["int", "uint", "range", "empty", "repeats"]
  410. and not isinstance(indices_dict[key], MultiIndex)
  411. ]
  412. )
  413. def index_with_missing(request):
  414. """
  415. Fixture for indices with missing values.
  416. Integer-dtype and empty cases are excluded because they cannot hold missing
  417. values.
  418. MultiIndex is excluded because isna() is not defined for MultiIndex.
  419. """
  420. # GH 35538. Use deep copy to avoid illusive bug on np-dev
  421. # Azure pipeline that writes into indices_dict despite copy
  422. ind = indices_dict[request.param].copy(deep=True)
  423. vals = ind.values
  424. if request.param in ["tuples", "mi-with-dt64tz-level", "multi"]:
  425. # For setting missing values in the top level of MultiIndex
  426. vals = ind.tolist()
  427. vals[0] = (None,) + vals[0][1:]
  428. vals[-1] = (None,) + vals[-1][1:]
  429. return MultiIndex.from_tuples(vals)
  430. else:
  431. vals[0] = None
  432. vals[-1] = None
  433. return type(ind)(vals)
  434. # ----------------------------------------------------------------
  435. # Series'
  436. # ----------------------------------------------------------------
  437. @pytest.fixture
  438. def empty_series():
  439. return Series([], index=[], dtype=np.float64)
  440. @pytest.fixture
  441. def string_series():
  442. """
  443. Fixture for Series of floats with Index of unique strings
  444. """
  445. s = tm.makeStringSeries()
  446. s.name = "series"
  447. return s
  448. @pytest.fixture
  449. def object_series():
  450. """
  451. Fixture for Series of dtype object with Index of unique strings
  452. """
  453. s = tm.makeObjectSeries()
  454. s.name = "objects"
  455. return s
  456. @pytest.fixture
  457. def datetime_series():
  458. """
  459. Fixture for Series of floats with DatetimeIndex
  460. """
  461. s = tm.makeTimeSeries()
  462. s.name = "ts"
  463. return s
  464. def _create_series(index):
  465. """Helper for the _series dict"""
  466. size = len(index)
  467. data = np.random.randn(size)
  468. return Series(data, index=index, name="a")
  469. _series = {
  470. f"series-with-{index_id}-index": _create_series(index)
  471. for index_id, index in indices_dict.items()
  472. }
  473. @pytest.fixture
  474. def series_with_simple_index(index):
  475. """
  476. Fixture for tests on series with changing types of indices.
  477. """
  478. return _create_series(index)
  479. @pytest.fixture
  480. def series_with_multilevel_index():
  481. """
  482. Fixture with a Series with a 2-level MultiIndex.
  483. """
  484. arrays = [
  485. ["bar", "bar", "baz", "baz", "qux", "qux", "foo", "foo"],
  486. ["one", "two", "one", "two", "one", "two", "one", "two"],
  487. ]
  488. tuples = zip(*arrays)
  489. index = MultiIndex.from_tuples(tuples)
  490. data = np.random.randn(8)
  491. ser = Series(data, index=index)
  492. ser[3] = np.NaN
  493. return ser
  494. _narrow_dtypes = [
  495. np.float16,
  496. np.float32,
  497. np.int8,
  498. np.int16,
  499. np.int32,
  500. np.uint8,
  501. np.uint16,
  502. np.uint32,
  503. ]
  504. _narrow_series = {
  505. f"{dtype.__name__}-series": tm.makeFloatSeries(name="a").astype(dtype)
  506. for dtype in _narrow_dtypes
  507. }
  508. @pytest.fixture(params=_narrow_series.keys())
  509. def narrow_series(request):
  510. """
  511. Fixture for Series with low precision data types
  512. """
  513. # copy to avoid mutation, e.g. setting .name
  514. return _narrow_series[request.param].copy()
  515. _index_or_series_objs = {**indices_dict, **_series, **_narrow_series}
  516. @pytest.fixture(params=_index_or_series_objs.keys())
  517. def index_or_series_obj(request):
  518. """
  519. Fixture for tests on indexes, series and series with a narrow dtype
  520. copy to avoid mutation, e.g. setting .name
  521. """
  522. return _index_or_series_objs[request.param].copy(deep=True)
  523. # ----------------------------------------------------------------
  524. # DataFrames
  525. # ----------------------------------------------------------------
  526. @pytest.fixture
  527. def empty_frame():
  528. return DataFrame()
  529. @pytest.fixture
  530. def int_frame():
  531. """
  532. Fixture for DataFrame of ints with index of unique strings
  533. Columns are ['A', 'B', 'C', 'D']
  534. A B C D
  535. vpBeWjM651 1 0 1 0
  536. 5JyxmrP1En -1 0 0 0
  537. qEDaoD49U2 -1 1 0 0
  538. m66TkTfsFe 0 0 0 0
  539. EHPaNzEUFm -1 0 -1 0
  540. fpRJCevQhi 2 0 0 0
  541. OlQvnmfi3Q 0 0 -2 0
  542. ... .. .. .. ..
  543. uB1FPlz4uP 0 0 0 1
  544. EcSe6yNzCU 0 0 -1 0
  545. L50VudaiI8 -1 1 -2 0
  546. y3bpw4nwIp 0 -1 0 0
  547. H0RdLLwrCT 1 1 0 0
  548. rY82K0vMwm 0 0 0 0
  549. 1OPIUjnkjk 2 0 0 0
  550. [30 rows x 4 columns]
  551. """
  552. return DataFrame(tm.getSeriesData()).astype("int64")
  553. @pytest.fixture
  554. def datetime_frame():
  555. """
  556. Fixture for DataFrame of floats with DatetimeIndex
  557. Columns are ['A', 'B', 'C', 'D']
  558. A B C D
  559. 2000-01-03 -1.122153 0.468535 0.122226 1.693711
  560. 2000-01-04 0.189378 0.486100 0.007864 -1.216052
  561. 2000-01-05 0.041401 -0.835752 -0.035279 -0.414357
  562. 2000-01-06 0.430050 0.894352 0.090719 0.036939
  563. 2000-01-07 -0.620982 -0.668211 -0.706153 1.466335
  564. 2000-01-10 -0.752633 0.328434 -0.815325 0.699674
  565. 2000-01-11 -2.236969 0.615737 -0.829076 -1.196106
  566. ... ... ... ... ...
  567. 2000-02-03 1.642618 -0.579288 0.046005 1.385249
  568. 2000-02-04 -0.544873 -1.160962 -0.284071 -1.418351
  569. 2000-02-07 -2.656149 -0.601387 1.410148 0.444150
  570. 2000-02-08 -1.201881 -1.289040 0.772992 -1.445300
  571. 2000-02-09 1.377373 0.398619 1.008453 -0.928207
  572. 2000-02-10 0.473194 -0.636677 0.984058 0.511519
  573. 2000-02-11 -0.965556 0.408313 -1.312844 -0.381948
  574. [30 rows x 4 columns]
  575. """
  576. return DataFrame(tm.getTimeSeriesData())
  577. @pytest.fixture
  578. def float_frame():
  579. """
  580. Fixture for DataFrame of floats with index of unique strings
  581. Columns are ['A', 'B', 'C', 'D'].
  582. A B C D
  583. P7GACiRnxd -0.465578 -0.361863 0.886172 -0.053465
  584. qZKh6afn8n -0.466693 -0.373773 0.266873 1.673901
  585. tkp0r6Qble 0.148691 -0.059051 0.174817 1.598433
  586. wP70WOCtv8 0.133045 -0.581994 -0.992240 0.261651
  587. M2AeYQMnCz -1.207959 -0.185775 0.588206 0.563938
  588. QEPzyGDYDo -0.381843 -0.758281 0.502575 -0.565053
  589. r78Jwns6dn -0.653707 0.883127 0.682199 0.206159
  590. ... ... ... ... ...
  591. IHEGx9NO0T -0.277360 0.113021 -1.018314 0.196316
  592. lPMj8K27FA -1.313667 -0.604776 -1.305618 -0.863999
  593. qa66YMWQa5 1.110525 0.475310 -0.747865 0.032121
  594. yOa0ATsmcE -0.431457 0.067094 0.096567 -0.264962
  595. 65znX3uRNG 1.528446 0.160416 -0.109635 -0.032987
  596. eCOBvKqf3e 0.235281 1.622222 0.781255 0.392871
  597. xSucinXxuV -1.263557 0.252799 -0.552247 0.400426
  598. [30 rows x 4 columns]
  599. """
  600. return DataFrame(tm.getSeriesData())
  601. @pytest.fixture
  602. def mixed_type_frame():
  603. """
  604. Fixture for DataFrame of float/int/string columns with RangeIndex
  605. Columns are ['a', 'b', 'c', 'float32', 'int32'].
  606. """
  607. return DataFrame(
  608. {
  609. "a": 1.0,
  610. "b": 2,
  611. "c": "foo",
  612. "float32": np.array([1.0] * 10, dtype="float32"),
  613. "int32": np.array([1] * 10, dtype="int32"),
  614. },
  615. index=np.arange(10),
  616. )
  617. @pytest.fixture
  618. def rand_series_with_duplicate_datetimeindex():
  619. """
  620. Fixture for Series with a DatetimeIndex that has duplicates.
  621. """
  622. dates = [
  623. datetime(2000, 1, 2),
  624. datetime(2000, 1, 2),
  625. datetime(2000, 1, 2),
  626. datetime(2000, 1, 3),
  627. datetime(2000, 1, 3),
  628. datetime(2000, 1, 3),
  629. datetime(2000, 1, 4),
  630. datetime(2000, 1, 4),
  631. datetime(2000, 1, 4),
  632. datetime(2000, 1, 5),
  633. ]
  634. return Series(np.random.randn(len(dates)), index=dates)
  635. # ----------------------------------------------------------------
  636. # Scalars
  637. # ----------------------------------------------------------------
  638. @pytest.fixture(
  639. params=[
  640. (Interval(left=0, right=5), IntervalDtype("int64", "right")),
  641. (Interval(left=0.1, right=0.5), IntervalDtype("float64", "right")),
  642. (Period("2012-01", freq="M"), "period[M]"),
  643. (Period("2012-02-01", freq="D"), "period[D]"),
  644. (
  645. Timestamp("2011-01-01", tz="US/Eastern"),
  646. DatetimeTZDtype(tz="US/Eastern"),
  647. ),
  648. (Timedelta(seconds=500), "timedelta64[ns]"),
  649. ]
  650. )
  651. def ea_scalar_and_dtype(request):
  652. return request.param
  653. # ----------------------------------------------------------------
  654. # Operators & Operations
  655. # ----------------------------------------------------------------
  656. _all_arithmetic_operators = [
  657. "__add__",
  658. "__radd__",
  659. "__sub__",
  660. "__rsub__",
  661. "__mul__",
  662. "__rmul__",
  663. "__floordiv__",
  664. "__rfloordiv__",
  665. "__truediv__",
  666. "__rtruediv__",
  667. "__pow__",
  668. "__rpow__",
  669. "__mod__",
  670. "__rmod__",
  671. ]
  672. @pytest.fixture(params=_all_arithmetic_operators)
  673. def all_arithmetic_operators(request):
  674. """
  675. Fixture for dunder names for common arithmetic operations.
  676. """
  677. return request.param
  678. @pytest.fixture(
  679. params=[
  680. operator.add,
  681. ops.radd,
  682. operator.sub,
  683. ops.rsub,
  684. operator.mul,
  685. ops.rmul,
  686. operator.truediv,
  687. ops.rtruediv,
  688. operator.floordiv,
  689. ops.rfloordiv,
  690. operator.mod,
  691. ops.rmod,
  692. operator.pow,
  693. ops.rpow,
  694. operator.eq,
  695. operator.ne,
  696. operator.lt,
  697. operator.le,
  698. operator.gt,
  699. operator.ge,
  700. operator.and_,
  701. ops.rand_,
  702. operator.xor,
  703. ops.rxor,
  704. operator.or_,
  705. ops.ror_,
  706. ]
  707. )
  708. def all_binary_operators(request):
  709. """
  710. Fixture for operator and roperator arithmetic, comparison, and logical ops.
  711. """
  712. return request.param
  713. @pytest.fixture(
  714. params=[
  715. operator.add,
  716. ops.radd,
  717. operator.sub,
  718. ops.rsub,
  719. operator.mul,
  720. ops.rmul,
  721. operator.truediv,
  722. ops.rtruediv,
  723. operator.floordiv,
  724. ops.rfloordiv,
  725. operator.mod,
  726. ops.rmod,
  727. operator.pow,
  728. ops.rpow,
  729. ]
  730. )
  731. def all_arithmetic_functions(request):
  732. """
  733. Fixture for operator and roperator arithmetic functions.
  734. Notes
  735. -----
  736. This includes divmod and rdivmod, whereas all_arithmetic_operators
  737. does not.
  738. """
  739. return request.param
  740. _all_numeric_reductions = [
  741. "sum",
  742. "max",
  743. "min",
  744. "mean",
  745. "prod",
  746. "std",
  747. "var",
  748. "median",
  749. "kurt",
  750. "skew",
  751. ]
  752. @pytest.fixture(params=_all_numeric_reductions)
  753. def all_numeric_reductions(request):
  754. """
  755. Fixture for numeric reduction names.
  756. """
  757. return request.param
  758. _all_boolean_reductions = ["all", "any"]
  759. @pytest.fixture(params=_all_boolean_reductions)
  760. def all_boolean_reductions(request):
  761. """
  762. Fixture for boolean reduction names.
  763. """
  764. return request.param
  765. _all_reductions = _all_numeric_reductions + _all_boolean_reductions
  766. @pytest.fixture(params=_all_reductions)
  767. def all_reductions(request):
  768. """
  769. Fixture for all (boolean + numeric) reduction names.
  770. """
  771. return request.param
  772. @pytest.fixture(params=["__eq__", "__ne__", "__le__", "__lt__", "__ge__", "__gt__"])
  773. def all_compare_operators(request):
  774. """
  775. Fixture for dunder names for common compare operations
  776. * >=
  777. * >
  778. * ==
  779. * !=
  780. * <
  781. * <=
  782. """
  783. return request.param
  784. @pytest.fixture(params=["__le__", "__lt__", "__ge__", "__gt__"])
  785. def compare_operators_no_eq_ne(request):
  786. """
  787. Fixture for dunder names for compare operations except == and !=
  788. * >=
  789. * >
  790. * <
  791. * <=
  792. """
  793. return request.param
  794. @pytest.fixture(
  795. params=["__and__", "__rand__", "__or__", "__ror__", "__xor__", "__rxor__"]
  796. )
  797. def all_logical_operators(request):
  798. """
  799. Fixture for dunder names for common logical operations
  800. * |
  801. * &
  802. * ^
  803. """
  804. return request.param
  805. # ----------------------------------------------------------------
  806. # Data sets/files
  807. # ----------------------------------------------------------------
  808. @pytest.fixture
  809. def strict_data_files(pytestconfig):
  810. """
  811. Returns the configuration for the test setting `--strict-data-files`.
  812. """
  813. return pytestconfig.getoption("--strict-data-files")
  814. @pytest.fixture
  815. def datapath(strict_data_files):
  816. """
  817. Get the path to a data file.
  818. Parameters
  819. ----------
  820. path : str
  821. Path to the file, relative to ``pandas/tests/``
  822. Returns
  823. -------
  824. path including ``pandas/tests``.
  825. Raises
  826. ------
  827. ValueError
  828. If the path doesn't exist and the --strict-data-files option is set.
  829. """
  830. BASE_PATH = os.path.join(os.path.dirname(__file__), "tests")
  831. def deco(*args):
  832. path = os.path.join(BASE_PATH, *args)
  833. if not os.path.exists(path):
  834. if strict_data_files:
  835. raise ValueError(
  836. f"Could not find file {path} and --strict-data-files is set."
  837. )
  838. else:
  839. pytest.skip(f"Could not find {path}.")
  840. return path
  841. return deco
  842. @pytest.fixture
  843. def iris(datapath):
  844. """
  845. The iris dataset as a DataFrame.
  846. """
  847. return pd.read_csv(datapath("io", "data", "csv", "iris.csv"))
  848. # ----------------------------------------------------------------
  849. # Time zones
  850. # ----------------------------------------------------------------
  851. TIMEZONES = [
  852. None,
  853. "UTC",
  854. "US/Eastern",
  855. "Asia/Tokyo",
  856. "dateutil/US/Pacific",
  857. "dateutil/Asia/Singapore",
  858. "+01:15",
  859. "-02:15",
  860. "UTC+01:15",
  861. "UTC-02:15",
  862. tzutc(),
  863. tzlocal(),
  864. FixedOffset(300),
  865. FixedOffset(0),
  866. FixedOffset(-300),
  867. timezone.utc,
  868. timezone(timedelta(hours=1)),
  869. timezone(timedelta(hours=-1), name="foo"),
  870. ]
  871. TIMEZONE_IDS = [repr(i) for i in TIMEZONES]
  872. @td.parametrize_fixture_doc(str(TIMEZONE_IDS))
  873. @pytest.fixture(params=TIMEZONES, ids=TIMEZONE_IDS)
  874. def tz_naive_fixture(request):
  875. """
  876. Fixture for trying timezones including default (None): {0}
  877. """
  878. return request.param
  879. @td.parametrize_fixture_doc(str(TIMEZONE_IDS[1:]))
  880. @pytest.fixture(params=TIMEZONES[1:], ids=TIMEZONE_IDS[1:])
  881. def tz_aware_fixture(request):
  882. """
  883. Fixture for trying explicit timezones: {0}
  884. """
  885. return request.param
  886. # Generate cartesian product of tz_aware_fixture:
  887. tz_aware_fixture2 = tz_aware_fixture
  888. @pytest.fixture(params=["utc", "dateutil/UTC", utc, tzutc(), timezone.utc])
  889. def utc_fixture(request):
  890. """
  891. Fixture to provide variants of UTC timezone strings and tzinfo objects.
  892. """
  893. return request.param
  894. utc_fixture2 = utc_fixture
  895. # ----------------------------------------------------------------
  896. # Dtypes
  897. # ----------------------------------------------------------------
  898. @pytest.fixture(params=tm.STRING_DTYPES)
  899. def string_dtype(request):
  900. """
  901. Parametrized fixture for string dtypes.
  902. * str
  903. * 'str'
  904. * 'U'
  905. """
  906. return request.param
  907. @pytest.fixture(
  908. params=[
  909. "string[python]",
  910. pytest.param(
  911. "string[pyarrow]", marks=td.skip_if_no("pyarrow", min_version="1.0.0")
  912. ),
  913. ]
  914. )
  915. def nullable_string_dtype(request):
  916. """
  917. Parametrized fixture for string dtypes.
  918. * 'string[python]'
  919. * 'string[pyarrow]'
  920. """
  921. return request.param
  922. @pytest.fixture(
  923. params=[
  924. "python",
  925. pytest.param("pyarrow", marks=td.skip_if_no("pyarrow", min_version="1.0.0")),
  926. ]
  927. )
  928. def string_storage(request):
  929. """
  930. Parametrized fixture for pd.options.mode.string_storage.
  931. * 'python'
  932. * 'pyarrow'
  933. """
  934. return request.param
  935. # Alias so we can test with cartesian product of string_storage
  936. string_storage2 = string_storage
  937. @pytest.fixture(params=tm.BYTES_DTYPES)
  938. def bytes_dtype(request):
  939. """
  940. Parametrized fixture for bytes dtypes.
  941. * bytes
  942. * 'bytes'
  943. """
  944. return request.param
  945. @pytest.fixture(params=tm.OBJECT_DTYPES)
  946. def object_dtype(request):
  947. """
  948. Parametrized fixture for object dtypes.
  949. * object
  950. * 'object'
  951. """
  952. return request.param
  953. @pytest.fixture(
  954. params=[
  955. "object",
  956. "string[python]",
  957. pytest.param(
  958. "string[pyarrow]", marks=td.skip_if_no("pyarrow", min_version="1.0.0")
  959. ),
  960. ]
  961. )
  962. def any_string_dtype(request):
  963. """
  964. Parametrized fixture for string dtypes.
  965. * 'object'
  966. * 'string[python]'
  967. * 'string[pyarrow]'
  968. """
  969. return request.param
  970. @pytest.fixture(params=tm.DATETIME64_DTYPES)
  971. def datetime64_dtype(request):
  972. """
  973. Parametrized fixture for datetime64 dtypes.
  974. * 'datetime64[ns]'
  975. * 'M8[ns]'
  976. """
  977. return request.param
  978. @pytest.fixture(params=tm.TIMEDELTA64_DTYPES)
  979. def timedelta64_dtype(request):
  980. """
  981. Parametrized fixture for timedelta64 dtypes.
  982. * 'timedelta64[ns]'
  983. * 'm8[ns]'
  984. """
  985. return request.param
  986. @pytest.fixture(params=tm.FLOAT_DTYPES)
  987. def float_dtype(request):
  988. """
  989. Parameterized fixture for float dtypes.
  990. * float
  991. * 'float32'
  992. * 'float64'
  993. """
  994. return request.param
  995. @pytest.fixture(params=tm.FLOAT_EA_DTYPES)
  996. def float_ea_dtype(request):
  997. """
  998. Parameterized fixture for float dtypes.
  999. * 'Float32'
  1000. * 'Float64'
  1001. """
  1002. return request.param
  1003. @pytest.fixture(params=tm.FLOAT_DTYPES + tm.FLOAT_EA_DTYPES)
  1004. def any_float_allowed_nullable_dtype(request):
  1005. """
  1006. Parameterized fixture for float dtypes.
  1007. * float
  1008. * 'float32'
  1009. * 'float64'
  1010. * 'Float32'
  1011. * 'Float64'
  1012. """
  1013. return request.param
  1014. @pytest.fixture(params=tm.COMPLEX_DTYPES)
  1015. def complex_dtype(request):
  1016. """
  1017. Parameterized fixture for complex dtypes.
  1018. * complex
  1019. * 'complex64'
  1020. * 'complex128'
  1021. """
  1022. return request.param
  1023. @pytest.fixture(params=tm.SIGNED_INT_DTYPES)
  1024. def sint_dtype(request):
  1025. """
  1026. Parameterized fixture for signed integer dtypes.
  1027. * int
  1028. * 'int8'
  1029. * 'int16'
  1030. * 'int32'
  1031. * 'int64'
  1032. """
  1033. return request.param
  1034. @pytest.fixture(params=tm.UNSIGNED_INT_DTYPES)
  1035. def uint_dtype(request):
  1036. """
  1037. Parameterized fixture for unsigned integer dtypes.
  1038. * 'uint8'
  1039. * 'uint16'
  1040. * 'uint32'
  1041. * 'uint64'
  1042. """
  1043. return request.param
  1044. @pytest.fixture(params=tm.ALL_INT_DTYPES)
  1045. def any_int_dtype(request):
  1046. """
  1047. Parameterized fixture for any integer dtype.
  1048. * int
  1049. * 'int8'
  1050. * 'uint8'
  1051. * 'int16'
  1052. * 'uint16'
  1053. * 'int32'
  1054. * 'uint32'
  1055. * 'int64'
  1056. * 'uint64'
  1057. """
  1058. return request.param
  1059. @pytest.fixture(params=tm.ALL_EA_INT_DTYPES)
  1060. def any_nullable_int_dtype(request):
  1061. """
  1062. Parameterized fixture for any nullable integer dtype.
  1063. * 'UInt8'
  1064. * 'Int8'
  1065. * 'UInt16'
  1066. * 'Int16'
  1067. * 'UInt32'
  1068. * 'Int32'
  1069. * 'UInt64'
  1070. * 'Int64'
  1071. """
  1072. return request.param
  1073. @pytest.fixture(params=tm.ALL_INT_DTYPES + tm.ALL_EA_INT_DTYPES)
  1074. def any_int_or_nullable_int_dtype(request):
  1075. """
  1076. Parameterized fixture for any nullable integer dtype.
  1077. * int
  1078. * 'int8'
  1079. * 'uint8'
  1080. * 'int16'
  1081. * 'uint16'
  1082. * 'int32'
  1083. * 'uint32'
  1084. * 'int64'
  1085. * 'uint64'
  1086. * 'UInt8'
  1087. * 'Int8'
  1088. * 'UInt16'
  1089. * 'Int16'
  1090. * 'UInt32'
  1091. * 'Int32'
  1092. * 'UInt64'
  1093. * 'Int64'
  1094. """
  1095. return request.param
  1096. @pytest.fixture(params=tm.ALL_EA_INT_DTYPES + tm.FLOAT_EA_DTYPES)
  1097. def any_nullable_numeric_dtype(request):
  1098. """
  1099. Parameterized fixture for any nullable integer dtype and
  1100. any float ea dtypes.
  1101. * 'UInt8'
  1102. * 'Int8'
  1103. * 'UInt16'
  1104. * 'Int16'
  1105. * 'UInt32'
  1106. * 'Int32'
  1107. * 'UInt64'
  1108. * 'Int64'
  1109. * 'Float32'
  1110. * 'Float64'
  1111. """
  1112. return request.param
  1113. @pytest.fixture(params=tm.SIGNED_EA_INT_DTYPES)
  1114. def any_signed_nullable_int_dtype(request):
  1115. """
  1116. Parameterized fixture for any signed nullable integer dtype.
  1117. * 'Int8'
  1118. * 'Int16'
  1119. * 'Int32'
  1120. * 'Int64'
  1121. """
  1122. return request.param
  1123. @pytest.fixture(params=tm.ALL_REAL_DTYPES)
  1124. def any_real_dtype(request):
  1125. """
  1126. Parameterized fixture for any (purely) real numeric dtype.
  1127. * int
  1128. * 'int8'
  1129. * 'uint8'
  1130. * 'int16'
  1131. * 'uint16'
  1132. * 'int32'
  1133. * 'uint32'
  1134. * 'int64'
  1135. * 'uint64'
  1136. * float
  1137. * 'float32'
  1138. * 'float64'
  1139. """
  1140. return request.param
  1141. @pytest.fixture(params=tm.ALL_NUMPY_DTYPES)
  1142. def any_numpy_dtype(request):
  1143. """
  1144. Parameterized fixture for all numpy dtypes.
  1145. * bool
  1146. * 'bool'
  1147. * int
  1148. * 'int8'
  1149. * 'uint8'
  1150. * 'int16'
  1151. * 'uint16'
  1152. * 'int32'
  1153. * 'uint32'
  1154. * 'int64'
  1155. * 'uint64'
  1156. * float
  1157. * 'float32'
  1158. * 'float64'
  1159. * complex
  1160. * 'complex64'
  1161. * 'complex128'
  1162. * str
  1163. * 'str'
  1164. * 'U'
  1165. * bytes
  1166. * 'bytes'
  1167. * 'datetime64[ns]'
  1168. * 'M8[ns]'
  1169. * 'timedelta64[ns]'
  1170. * 'm8[ns]'
  1171. * object
  1172. * 'object'
  1173. """
  1174. return request.param
  1175. # categoricals are handled separately
  1176. _any_skipna_inferred_dtype = [
  1177. ("string", ["a", np.nan, "c"]),
  1178. ("string", ["a", pd.NA, "c"]),
  1179. ("bytes", [b"a", np.nan, b"c"]),
  1180. ("empty", [np.nan, np.nan, np.nan]),
  1181. ("empty", []),
  1182. ("mixed-integer", ["a", np.nan, 2]),
  1183. ("mixed", ["a", np.nan, 2.0]),
  1184. ("floating", [1.0, np.nan, 2.0]),
  1185. ("integer", [1, np.nan, 2]),
  1186. ("mixed-integer-float", [1, np.nan, 2.0]),
  1187. ("decimal", [Decimal(1), np.nan, Decimal(2)]),
  1188. ("boolean", [True, np.nan, False]),
  1189. ("boolean", [True, pd.NA, False]),
  1190. ("datetime64", [np.datetime64("2013-01-01"), np.nan, np.datetime64("2018-01-01")]),
  1191. ("datetime", [Timestamp("20130101"), np.nan, Timestamp("20180101")]),
  1192. ("date", [date(2013, 1, 1), np.nan, date(2018, 1, 1)]),
  1193. # The following two dtypes are commented out due to GH 23554
  1194. # ('complex', [1 + 1j, np.nan, 2 + 2j]),
  1195. # ('timedelta64', [np.timedelta64(1, 'D'),
  1196. # np.nan, np.timedelta64(2, 'D')]),
  1197. ("timedelta", [timedelta(1), np.nan, timedelta(2)]),
  1198. ("time", [time(1), np.nan, time(2)]),
  1199. ("period", [Period(2013), pd.NaT, Period(2018)]),
  1200. ("interval", [Interval(0, 1), np.nan, Interval(0, 2)]),
  1201. ]
  1202. ids, _ = zip(*_any_skipna_inferred_dtype) # use inferred type as fixture-id
  1203. @pytest.fixture(params=_any_skipna_inferred_dtype, ids=ids)
  1204. def any_skipna_inferred_dtype(request):
  1205. """
  1206. Fixture for all inferred dtypes from _libs.lib.infer_dtype
  1207. The covered (inferred) types are:
  1208. * 'string'
  1209. * 'empty'
  1210. * 'bytes'
  1211. * 'mixed'
  1212. * 'mixed-integer'
  1213. * 'mixed-integer-float'
  1214. * 'floating'
  1215. * 'integer'
  1216. * 'decimal'
  1217. * 'boolean'
  1218. * 'datetime64'
  1219. * 'datetime'
  1220. * 'date'
  1221. * 'timedelta'
  1222. * 'time'
  1223. * 'period'
  1224. * 'interval'
  1225. Returns
  1226. -------
  1227. inferred_dtype : str
  1228. The string for the inferred dtype from _libs.lib.infer_dtype
  1229. values : np.ndarray
  1230. An array of object dtype that will be inferred to have
  1231. `inferred_dtype`
  1232. Examples
  1233. --------
  1234. >>> import pandas._libs.lib as lib
  1235. >>>
  1236. >>> def test_something(any_skipna_inferred_dtype):
  1237. ... inferred_dtype, values = any_skipna_inferred_dtype
  1238. ... # will pass
  1239. ... assert lib.infer_dtype(values, skipna=True) == inferred_dtype
  1240. """
  1241. inferred_dtype, values = request.param
  1242. values = np.array(values, dtype=object) # object dtype to avoid casting
  1243. # correctness of inference tested in tests/dtypes/test_inference.py
  1244. return inferred_dtype, values
  1245. # ----------------------------------------------------------------
  1246. # Misc
  1247. # ----------------------------------------------------------------
  1248. @pytest.fixture
  1249. def ip():
  1250. """
  1251. Get an instance of IPython.InteractiveShell.
  1252. Will raise a skip if IPython is not installed.
  1253. """
  1254. pytest.importorskip("IPython", minversion="6.0.0")
  1255. from IPython.core.interactiveshell import InteractiveShell
  1256. # GH#35711 make sure sqlite history file handle is not leaked
  1257. from traitlets.config import Config # isort:skip
  1258. c = Config()
  1259. c.HistoryManager.hist_file = ":memory:"
  1260. return InteractiveShell(config=c)
  1261. @pytest.fixture(params=["bsr", "coo", "csc", "csr", "dia", "dok", "lil"])
  1262. def spmatrix(request):
  1263. """
  1264. Yields scipy sparse matrix classes.
  1265. """
  1266. from scipy import sparse
  1267. return getattr(sparse, request.param + "_matrix")
  1268. @pytest.fixture(
  1269. params=[
  1270. getattr(pd.offsets, o)
  1271. for o in pd.offsets.__all__
  1272. if issubclass(getattr(pd.offsets, o), pd.offsets.Tick)
  1273. ]
  1274. )
  1275. def tick_classes(request):
  1276. """
  1277. Fixture for Tick based datetime offsets available for a time series.
  1278. """
  1279. return request.param
  1280. @pytest.fixture(params=[None, lambda x: x])
  1281. def sort_by_key(request):
  1282. """
  1283. Simple fixture for testing keys in sorting methods.
  1284. Tests None (no key) and the identity key.
  1285. """
  1286. return request.param
  1287. @pytest.fixture()
  1288. def fsspectest():
  1289. pytest.importorskip("fsspec")
  1290. from fsspec import register_implementation
  1291. from fsspec.implementations.memory import MemoryFileSystem
  1292. from fsspec.registry import _registry as registry
  1293. class TestMemoryFS(MemoryFileSystem):
  1294. protocol = "testmem"
  1295. test = [None]
  1296. def __init__(self, **kwargs):
  1297. self.test[0] = kwargs.pop("test", None)
  1298. super().__init__(**kwargs)
  1299. register_implementation("testmem", TestMemoryFS, clobber=True)
  1300. yield TestMemoryFS()
  1301. registry.pop("testmem", None)
  1302. TestMemoryFS.test[0] = None
  1303. TestMemoryFS.store.clear()
  1304. @pytest.fixture(
  1305. params=[
  1306. ("foo", None, None),
  1307. ("Egon", "Venkman", None),
  1308. ("NCC1701D", "NCC1701D", "NCC1701D"),
  1309. ]
  1310. )
  1311. def names(request):
  1312. """
  1313. A 3-tuple of names, the first two for operands, the last for a result.
  1314. """
  1315. return request.param
  1316. @pytest.fixture(params=[tm.setitem, tm.loc, tm.iloc])
  1317. def indexer_sli(request):
  1318. """
  1319. Parametrize over __setitem__, loc.__setitem__, iloc.__setitem__
  1320. """
  1321. return request.param
  1322. @pytest.fixture(params=[tm.setitem, tm.iloc])
  1323. def indexer_si(request):
  1324. """
  1325. Parametrize over __setitem__, iloc.__setitem__
  1326. """
  1327. return request.param
  1328. @pytest.fixture(params=[tm.setitem, tm.loc])
  1329. def indexer_sl(request):
  1330. """
  1331. Parametrize over __setitem__, loc.__setitem__
  1332. """
  1333. return request.param
  1334. @pytest.fixture(params=[tm.at, tm.loc])
  1335. def indexer_al(request):
  1336. """
  1337. Parametrize over at.__setitem__, loc.__setitem__
  1338. """
  1339. return request.param
  1340. @pytest.fixture
  1341. def using_array_manager(request):
  1342. """
  1343. Fixture to check if the array manager is being used.
  1344. """
  1345. return pd.options.mode.data_manager == "array"