diff --git a/asv_bench/benchmarks/algorithms.py b/asv_bench/benchmarks/algorithms.py index 3c78b0a9a60c8..933e8fbc175d8 100644 --- a/asv_bench/benchmarks/algorithms.py +++ b/asv_bench/benchmarks/algorithms.py @@ -4,8 +4,6 @@ import pandas as pd -from .pandas_vb_common import tm - for imp in ["pandas.util", "pandas.tools.hashing"]: try: hashing = import_module(imp) @@ -47,9 +45,12 @@ def setup(self, unique, sort, dtype): elif dtype == "datetime64[ns, tz]": data = pd.date_range("2011-01-01", freq="h", periods=N, tz="Asia/Tokyo") elif dtype == "object_str": - data = tm.makeStringIndex(N) + data = pd.Index([f"i-{i}" for i in range(N)], dtype=object) elif dtype == "string[pyarrow]": - data = pd.array(tm.makeStringIndex(N), dtype="string[pyarrow]") + data = pd.array( + pd.Index([f"i-{i}" for i in range(N)], dtype=object), + dtype="string[pyarrow]", + ) else: raise NotImplementedError @@ -88,7 +89,7 @@ def setup(self, unique, keep, dtype): elif dtype == "float64": data = pd.Index(np.random.randn(N), dtype="float64") elif dtype == "string": - data = tm.makeStringIndex(N) + data = pd.Index([f"i-{i}" for i in range(N)], dtype=object) elif dtype == "datetime64[ns]": data = pd.date_range("2011-01-01", freq="h", periods=N) elif dtype == "datetime64[ns, tz]": @@ -136,7 +137,9 @@ def setup_cache(self): df = pd.DataFrame( { "strings": pd.Series( - tm.makeStringIndex(10000).take(np.random.randint(0, 10000, size=N)) + pd.Index([f"i-{i}" for i in range(10000)], dtype=object).take( + np.random.randint(0, 10000, size=N) + ) ), "floats": np.random.randn(N), "ints": np.arange(N), diff --git a/asv_bench/benchmarks/algos/isin.py b/asv_bench/benchmarks/algos/isin.py index 92797425b2c30..2b3d32fb579dc 100644 --- a/asv_bench/benchmarks/algos/isin.py +++ b/asv_bench/benchmarks/algos/isin.py @@ -8,8 +8,6 @@ date_range, ) -from ..pandas_vb_common import tm - class IsIn: params = [ @@ -60,7 +58,9 @@ def setup(self, dtype): elif dtype in ["str", "string[python]", "string[pyarrow]"]: try: - self.series = Series(tm.makeStringIndex(N), dtype=dtype) + self.series = Series( + Index([f"i-{i}" for i in range(N)], dtype=object), dtype=dtype + ) except ImportError: raise NotImplementedError self.values = list(self.series[:2]) diff --git a/asv_bench/benchmarks/ctors.py b/asv_bench/benchmarks/ctors.py index 2db00cc7f2ad9..77c9faf3d3a87 100644 --- a/asv_bench/benchmarks/ctors.py +++ b/asv_bench/benchmarks/ctors.py @@ -9,8 +9,6 @@ date_range, ) -from .pandas_vb_common import tm - def no_change(arr): return arr @@ -115,7 +113,7 @@ def time_dtindex_from_index_with_series(self): class MultiIndexConstructor: def setup(self): N = 10**4 - self.iterables = [tm.makeStringIndex(N), range(20)] + self.iterables = [Index([f"i-{i}" for i in range(N)], dtype=object), range(20)] def time_multiindex_from_iterables(self): MultiIndex.from_product(self.iterables) diff --git a/asv_bench/benchmarks/dtypes.py b/asv_bench/benchmarks/dtypes.py index c33043c0eddc1..7f3429b5e3882 100644 --- a/asv_bench/benchmarks/dtypes.py +++ b/asv_bench/benchmarks/dtypes.py @@ -3,7 +3,10 @@ import numpy as np import pandas as pd -from pandas import DataFrame +from pandas import ( + DataFrame, + Index, +) import pandas._testing as tm from pandas.api.types import ( is_extension_array_dtype, @@ -73,8 +76,8 @@ class SelectDtypes: def setup(self, dtype): N, K = 5000, 50 - self.index = tm.makeStringIndex(N) - self.columns = tm.makeStringIndex(K) + self.index = Index([f"i-{i}" for i in range(N)], dtype=object) + self.columns = Index([f"i-{i}" for i in range(K)], dtype=object) def create_df(data): return DataFrame(data, index=self.index, columns=self.columns) diff --git a/asv_bench/benchmarks/frame_ctor.py b/asv_bench/benchmarks/frame_ctor.py index 7092a679b8cf0..f938f7eb0d951 100644 --- a/asv_bench/benchmarks/frame_ctor.py +++ b/asv_bench/benchmarks/frame_ctor.py @@ -12,8 +12,6 @@ date_range, ) -from .pandas_vb_common import tm - try: from pandas.tseries.offsets import ( Hour, @@ -30,8 +28,8 @@ class FromDicts: def setup(self): N, K = 5000, 50 - self.index = tm.makeStringIndex(N) - self.columns = tm.makeStringIndex(K) + self.index = pd.Index([f"i-{i}" for i in range(N)], dtype=object) + self.columns = pd.Index([f"i-{i}" for i in range(K)], dtype=object) frame = DataFrame(np.random.randn(N, K), index=self.index, columns=self.columns) self.data = frame.to_dict() self.dict_list = frame.to_dict(orient="records") diff --git a/asv_bench/benchmarks/frame_methods.py b/asv_bench/benchmarks/frame_methods.py index f22a261041e17..a283afd1f0f1e 100644 --- a/asv_bench/benchmarks/frame_methods.py +++ b/asv_bench/benchmarks/frame_methods.py @@ -5,6 +5,7 @@ from pandas import ( DataFrame, + Index, MultiIndex, NaT, Series, @@ -14,8 +15,6 @@ timedelta_range, ) -from .pandas_vb_common import tm - class AsType: params = [ @@ -703,8 +702,12 @@ def setup(self, monotonic): K = 10 df = DataFrame( { - "key1": tm.makeStringIndex(N).values.repeat(K), - "key2": tm.makeStringIndex(N).values.repeat(K), + "key1": Index([f"i-{i}" for i in range(N)], dtype=object).values.repeat( + K + ), + "key2": Index([f"i-{i}" for i in range(N)], dtype=object).values.repeat( + K + ), "value": np.random.randn(N * K), } ) diff --git a/asv_bench/benchmarks/gil.py b/asv_bench/benchmarks/gil.py index c78819f75c52a..a0c4189c72d0e 100644 --- a/asv_bench/benchmarks/gil.py +++ b/asv_bench/benchmarks/gil.py @@ -5,6 +5,7 @@ from pandas import ( DataFrame, + Index, Series, date_range, factorize, @@ -12,8 +13,6 @@ ) from pandas.core.algorithms import take_nd -from .pandas_vb_common import tm - try: from pandas import ( rolling_kurt, @@ -34,7 +33,6 @@ except ImportError: from pandas import algos - from .pandas_vb_common import BaseIO # isort:skip @@ -305,7 +303,7 @@ class ParallelFactorize: param_names = ["threads"] def setup(self, threads): - strings = tm.makeStringIndex(100000) + strings = Index([f"i-{i}" for i in range(100000)], dtype=object) @test_parallel(num_threads=threads) def parallel(): diff --git a/asv_bench/benchmarks/groupby.py b/asv_bench/benchmarks/groupby.py index 202ba6e981b70..abffa1f702b9c 100644 --- a/asv_bench/benchmarks/groupby.py +++ b/asv_bench/benchmarks/groupby.py @@ -17,8 +17,6 @@ to_timedelta, ) -from .pandas_vb_common import tm - method_blocklist = { "object": { "diff", @@ -167,10 +165,14 @@ def setup_cache(self): "int64_small": Series(np.random.randint(0, 100, size=size)), "int64_large": Series(np.random.randint(0, 10000, size=size)), "object_small": Series( - tm.makeStringIndex(100).take(np.random.randint(0, 100, size=size)) + Index([f"i-{i}" for i in range(100)], dtype=object).take( + np.random.randint(0, 100, size=size) + ) ), "object_large": Series( - tm.makeStringIndex(10000).take(np.random.randint(0, 10000, size=size)) + Index([f"i-{i}" for i in range(10000)], dtype=object).take( + np.random.randint(0, 10000, size=size) + ) ), } return data @@ -912,7 +914,7 @@ def setup(self): n1 = 400 n2 = 250 index = MultiIndex( - levels=[np.arange(n1), tm.makeStringIndex(n2)], + levels=[np.arange(n1), Index([f"i-{i}" for i in range(n2)], dtype=object)], codes=[np.repeat(range(n1), n2).tolist(), list(range(n2)) * n1], names=["lev1", "lev2"], ) diff --git a/asv_bench/benchmarks/index_object.py b/asv_bench/benchmarks/index_object.py index 7e33223260e0f..637b1b40f59a3 100644 --- a/asv_bench/benchmarks/index_object.py +++ b/asv_bench/benchmarks/index_object.py @@ -12,8 +12,6 @@ date_range, ) -from .pandas_vb_common import tm - class SetOperations: params = ( @@ -30,7 +28,7 @@ def setup(self, index_structure, dtype, method): date_str_left = Index(dates_left.strftime(fmt)) int_left = Index(np.arange(N)) ea_int_left = Index(np.arange(N), dtype="Int64") - str_left = tm.makeStringIndex(N) + str_left = Index([f"i-{i}" for i in range(N)], dtype=object) data = { "datetime": dates_left, @@ -155,7 +153,12 @@ class Indexing: def setup(self, dtype): N = 10**6 - self.idx = getattr(tm, f"make{dtype}Index")(N) + if dtype == "String": + self.idx = Index([f"i-{i}" for i in range(N)], dtype=object) + elif dtype == "Float": + self.idx = Index(np.arange(N), dtype=np.float64) + elif dtype == "Int": + self.idx = Index(np.arange(N), dtype=np.int64) self.array_mask = (np.arange(N) % 3) == 0 self.series_mask = Series(self.array_mask) self.sorted = self.idx.sort_values() diff --git a/asv_bench/benchmarks/indexing.py b/asv_bench/benchmarks/indexing.py index 4961722c0e9cd..9ad1f5b31016d 100644 --- a/asv_bench/benchmarks/indexing.py +++ b/asv_bench/benchmarks/indexing.py @@ -22,8 +22,6 @@ period_range, ) -from .pandas_vb_common import tm - class NumericSeriesIndexing: params = [ @@ -124,7 +122,7 @@ class NonNumericSeriesIndexing: def setup(self, index, index_structure): N = 10**6 if index == "string": - index = tm.makeStringIndex(N) + index = Index([f"i-{i}" for i in range(N)], dtype=object) elif index == "datetime": index = date_range("1900", periods=N, freq="s") elif index == "period": @@ -156,8 +154,8 @@ def time_getitem_list_like(self, index, index_structure): class DataFrameStringIndexing: def setup(self): - index = tm.makeStringIndex(1000) - columns = tm.makeStringIndex(30) + index = Index([f"i-{i}" for i in range(1000)], dtype=object) + columns = Index([f"i-{i}" for i in range(30)], dtype=object) with warnings.catch_warnings(record=True): self.df = DataFrame(np.random.randn(1000, 30), index=index, columns=columns) self.idx_scalar = index[100] diff --git a/asv_bench/benchmarks/inference.py b/asv_bench/benchmarks/inference.py index 805b0c807452c..d5c58033c1157 100644 --- a/asv_bench/benchmarks/inference.py +++ b/asv_bench/benchmarks/inference.py @@ -9,6 +9,7 @@ import numpy as np from pandas import ( + Index, NaT, Series, date_range, @@ -17,10 +18,7 @@ to_timedelta, ) -from .pandas_vb_common import ( - lib, - tm, -) +from .pandas_vb_common import lib class ToNumeric: @@ -31,7 +29,7 @@ def setup(self, errors): N = 10000 self.float = Series(np.random.randn(N)) self.numstr = self.float.astype("str") - self.str = Series(tm.makeStringIndex(N)) + self.str = Series(Index([f"i-{i}" for i in range(N)], dtype=object)) def time_from_float(self, errors): to_numeric(self.float, errors=errors) diff --git a/asv_bench/benchmarks/io/csv.py b/asv_bench/benchmarks/io/csv.py index a45315f63d62e..9ac83db4f85b9 100644 --- a/asv_bench/benchmarks/io/csv.py +++ b/asv_bench/benchmarks/io/csv.py @@ -10,6 +10,7 @@ from pandas import ( Categorical, DataFrame, + Index, concat, date_range, period_range, @@ -17,10 +18,7 @@ to_datetime, ) -from ..pandas_vb_common import ( - BaseIO, - tm, -) +from ..pandas_vb_common import BaseIO class ToCSV(BaseIO): @@ -288,7 +286,7 @@ class ReadCSVSkipRows(BaseIO): def setup(self, skiprows, engine): N = 20000 - index = tm.makeStringIndex(N) + index = Index([f"i-{i}" for i in range(N)], dtype=object) df = DataFrame( { "float1": np.random.randn(N), diff --git a/asv_bench/benchmarks/io/excel.py b/asv_bench/benchmarks/io/excel.py index f8d81b0f6a699..902a61be901bd 100644 --- a/asv_bench/benchmarks/io/excel.py +++ b/asv_bench/benchmarks/io/excel.py @@ -12,12 +12,11 @@ from pandas import ( DataFrame, ExcelWriter, + Index, date_range, read_excel, ) -from ..pandas_vb_common import tm - def _generate_dataframe(): N = 2000 @@ -27,7 +26,7 @@ def _generate_dataframe(): columns=[f"float{i}" for i in range(C)], index=date_range("20000101", periods=N, freq="h"), ) - df["object"] = tm.makeStringIndex(N) + df["object"] = Index([f"i-{i}" for i in range(N)], dtype=object) return df diff --git a/asv_bench/benchmarks/io/hdf.py b/asv_bench/benchmarks/io/hdf.py index 195aaa158e178..acf0ec4b9d359 100644 --- a/asv_bench/benchmarks/io/hdf.py +++ b/asv_bench/benchmarks/io/hdf.py @@ -3,20 +3,18 @@ from pandas import ( DataFrame, HDFStore, + Index, date_range, read_hdf, ) -from ..pandas_vb_common import ( - BaseIO, - tm, -) +from ..pandas_vb_common import BaseIO class HDFStoreDataFrame(BaseIO): def setup(self): N = 25000 - index = tm.makeStringIndex(N) + index = Index([f"i-{i}" for i in range(N)], dtype=object) self.df = DataFrame( {"float1": np.random.randn(N), "float2": np.random.randn(N)}, index=index ) @@ -124,7 +122,7 @@ def setup(self, format): columns=[f"float{i}" for i in range(C)], index=date_range("20000101", periods=N, freq="h"), ) - self.df["object"] = tm.makeStringIndex(N) + self.df["object"] = Index([f"i-{i}" for i in range(N)], dtype=object) self.df.to_hdf(self.fname, "df", format=format) # Numeric df diff --git a/asv_bench/benchmarks/io/json.py b/asv_bench/benchmarks/io/json.py index 8a2e3fa87eb37..bcbfcdea42dd9 100644 --- a/asv_bench/benchmarks/io/json.py +++ b/asv_bench/benchmarks/io/json.py @@ -4,6 +4,7 @@ from pandas import ( DataFrame, + Index, concat, date_range, json_normalize, @@ -11,10 +12,7 @@ timedelta_range, ) -from ..pandas_vb_common import ( - BaseIO, - tm, -) +from ..pandas_vb_common import BaseIO class ReadJSON(BaseIO): @@ -114,7 +112,7 @@ def setup(self, orient, frame): ints = np.random.randint(100000000, size=N) longints = sys.maxsize * np.random.randint(100000000, size=N) floats = np.random.randn(N) - strings = tm.makeStringIndex(N) + strings = Index([f"i-{i}" for i in range(N)], dtype=object) self.df = DataFrame(np.random.randn(N, ncols), index=np.arange(N)) self.df_date_idx = DataFrame(np.random.randn(N, ncols), index=index) self.df_td_int_ts = DataFrame( @@ -220,7 +218,7 @@ def setup(self): ints = np.random.randint(100000000, size=N) longints = sys.maxsize * np.random.randint(100000000, size=N) floats = np.random.randn(N) - strings = tm.makeStringIndex(N) + strings = Index([f"i-{i}" for i in range(N)], dtype=object) self.df = DataFrame(np.random.randn(N, ncols), index=np.arange(N)) self.df_date_idx = DataFrame(np.random.randn(N, ncols), index=index) self.df_td_int_ts = DataFrame( diff --git a/asv_bench/benchmarks/io/pickle.py b/asv_bench/benchmarks/io/pickle.py index 54631d9236887..4787b57b54756 100644 --- a/asv_bench/benchmarks/io/pickle.py +++ b/asv_bench/benchmarks/io/pickle.py @@ -2,14 +2,12 @@ from pandas import ( DataFrame, + Index, date_range, read_pickle, ) -from ..pandas_vb_common import ( - BaseIO, - tm, -) +from ..pandas_vb_common import BaseIO class Pickle(BaseIO): @@ -22,7 +20,7 @@ def setup(self): columns=[f"float{i}" for i in range(C)], index=date_range("20000101", periods=N, freq="h"), ) - self.df["object"] = tm.makeStringIndex(N) + self.df["object"] = Index([f"i-{i}" for i in range(N)], dtype=object) self.df.to_pickle(self.fname) def time_read_pickle(self): diff --git a/asv_bench/benchmarks/io/sql.py b/asv_bench/benchmarks/io/sql.py index 6f893ee72d918..e87cc4aaa80c7 100644 --- a/asv_bench/benchmarks/io/sql.py +++ b/asv_bench/benchmarks/io/sql.py @@ -5,13 +5,12 @@ from pandas import ( DataFrame, + Index, date_range, read_sql_query, read_sql_table, ) -from ..pandas_vb_common import tm - class SQL: params = ["sqlalchemy", "sqlite"] @@ -35,7 +34,7 @@ def setup(self, connection): "int": np.random.randint(0, N, size=N), "datetime": date_range("2000-01-01", periods=N, freq="s"), }, - index=tm.makeStringIndex(N), + index=Index([f"i-{i}" for i in range(N)], dtype=object), ) self.df.iloc[1000:3000, 1] = np.nan self.df["date"] = self.df["datetime"].dt.date @@ -84,7 +83,7 @@ def setup(self, connection, dtype): "int": np.random.randint(0, N, size=N), "datetime": date_range("2000-01-01", periods=N, freq="s"), }, - index=tm.makeStringIndex(N), + index=Index([f"i-{i}" for i in range(N)], dtype=object), ) self.df.iloc[1000:3000, 1] = np.nan self.df["date"] = self.df["datetime"].dt.date @@ -113,7 +112,7 @@ def setup(self): "int": np.random.randint(0, N, size=N), "datetime": date_range("2000-01-01", periods=N, freq="s"), }, - index=tm.makeStringIndex(N), + index=Index([f"i-{i}" for i in range(N)], dtype=object), ) self.df.iloc[1000:3000, 1] = np.nan self.df["date"] = self.df["datetime"].dt.date @@ -159,7 +158,7 @@ def setup(self, dtype): "int": np.random.randint(0, N, size=N), "datetime": date_range("2000-01-01", periods=N, freq="s"), }, - index=tm.makeStringIndex(N), + index=Index([f"i-{i}" for i in range(N)], dtype=object), ) self.df.iloc[1000:3000, 1] = np.nan self.df["date"] = self.df["datetime"].dt.date diff --git a/asv_bench/benchmarks/io/stata.py b/asv_bench/benchmarks/io/stata.py index 750bcf4ccee5c..ff33ededdfed9 100644 --- a/asv_bench/benchmarks/io/stata.py +++ b/asv_bench/benchmarks/io/stata.py @@ -2,14 +2,12 @@ from pandas import ( DataFrame, + Index, date_range, read_stata, ) -from ..pandas_vb_common import ( - BaseIO, - tm, -) +from ..pandas_vb_common import BaseIO class Stata(BaseIO): @@ -25,7 +23,7 @@ def setup(self, convert_dates): columns=[f"float{i}" for i in range(C)], index=date_range("20000101", periods=N, freq="h"), ) - self.df["object"] = tm.makeStringIndex(self.N) + self.df["object"] = Index([f"i-{i}" for i in range(self.N)], dtype=object) self.df["int8_"] = np.random.randint( np.iinfo(np.int8).min, np.iinfo(np.int8).max - 27, N ) diff --git a/asv_bench/benchmarks/join_merge.py b/asv_bench/benchmarks/join_merge.py index 23824c2c748df..6f494562103c2 100644 --- a/asv_bench/benchmarks/join_merge.py +++ b/asv_bench/benchmarks/join_merge.py @@ -14,8 +14,6 @@ merge_asof, ) -from .pandas_vb_common import tm - try: from pandas import merge_ordered except ImportError: @@ -28,7 +26,7 @@ class Concat: def setup(self, axis): N = 1000 - s = Series(N, index=tm.makeStringIndex(N)) + s = Series(N, index=Index([f"i-{i}" for i in range(N)], dtype=object)) self.series = [s[i:-i] for i in range(1, 10)] * 50 self.small_frames = [DataFrame(np.random.randn(5, 4))] * 1000 df = DataFrame( @@ -94,7 +92,7 @@ def setup(self, dtype, structure, axis, sort): elif dtype in ("int64", "Int64", "int64[pyarrow]"): vals = np.arange(N, dtype=np.int64) elif dtype in ("string[python]", "string[pyarrow]"): - vals = tm.makeStringIndex(N) + vals = Index([f"i-{i}" for i in range(N)], dtype=object) else: raise NotImplementedError @@ -122,8 +120,8 @@ class Join: param_names = ["sort"] def setup(self, sort): - level1 = tm.makeStringIndex(10).values - level2 = tm.makeStringIndex(1000).values + level1 = Index([f"i-{i}" for i in range(10)], dtype=object).values + level2 = Index([f"i-{i}" for i in range(1000)], dtype=object).values codes1 = np.arange(10).repeat(1000) codes2 = np.tile(np.arange(1000), 10) index2 = MultiIndex(levels=[level1, level2], codes=[codes1, codes2]) @@ -231,8 +229,8 @@ class Merge: def setup(self, sort): N = 10000 - indices = tm.makeStringIndex(N).values - indices2 = tm.makeStringIndex(N).values + indices = Index([f"i-{i}" for i in range(N)], dtype=object).values + indices2 = Index([f"i-{i}" for i in range(N)], dtype=object).values key = np.tile(indices[:8000], 10) key2 = np.tile(indices2[:8000], 10) self.left = DataFrame( @@ -400,7 +398,7 @@ def time_merge_on_cat_idx(self): class MergeOrdered: def setup(self): - groups = tm.makeStringIndex(10).values + groups = Index([f"i-{i}" for i in range(10)], dtype=object).values self.left = DataFrame( { "group": groups.repeat(5000), diff --git a/asv_bench/benchmarks/libs.py b/asv_bench/benchmarks/libs.py index f041499c9c622..3419163bcfe09 100644 --- a/asv_bench/benchmarks/libs.py +++ b/asv_bench/benchmarks/libs.py @@ -15,13 +15,11 @@ from pandas import ( NA, + Index, NaT, ) -from .pandas_vb_common import ( - lib, - tm, -) +from .pandas_vb_common import lib try: from pandas.util import cache_readonly @@ -61,8 +59,8 @@ class FastZip: def setup(self): N = 10000 K = 10 - key1 = tm.makeStringIndex(N).values.repeat(K) - key2 = tm.makeStringIndex(N).values.repeat(K) + key1 = Index([f"i-{i}" for i in range(N)], dtype=object).values.repeat(K) + key2 = Index([f"i-{i}" for i in range(N)], dtype=object).values.repeat(K) col_array = np.vstack([key1, key2, np.random.randn(N * K)]) col_array2 = col_array.copy() col_array2[:, :10000] = np.nan diff --git a/asv_bench/benchmarks/multiindex_object.py b/asv_bench/benchmarks/multiindex_object.py index 87dcdb16fa647..54788d41d83fe 100644 --- a/asv_bench/benchmarks/multiindex_object.py +++ b/asv_bench/benchmarks/multiindex_object.py @@ -5,6 +5,7 @@ from pandas import ( NA, DataFrame, + Index, MultiIndex, RangeIndex, Series, @@ -12,8 +13,6 @@ date_range, ) -from .pandas_vb_common import tm - class GetLoc: def setup(self): @@ -144,7 +143,11 @@ def time_is_monotonic(self): class Duplicated: def setup(self): n, k = 200, 5000 - levels = [np.arange(n), tm.makeStringIndex(n).values, 1000 + np.arange(n)] + levels = [ + np.arange(n), + Index([f"i-{i}" for i in range(n)], dtype=object).values, + 1000 + np.arange(n), + ] codes = [np.random.choice(n, (k * n)) for lev in levels] self.mi = MultiIndex(levels=levels, codes=codes) @@ -249,7 +252,7 @@ def setup(self, index_structure, dtype, method, sort): level2 = range(N // 1000) int_left = MultiIndex.from_product([level1, level2]) - level2 = tm.makeStringIndex(N // 1000).values + level2 = Index([f"i-{i}" for i in range(N // 1000)], dtype=object).values str_left = MultiIndex.from_product([level1, level2]) level2 = range(N // 1000) @@ -293,7 +296,7 @@ def setup(self, dtype): level2[0] = NA ea_int_left = MultiIndex.from_product([level1, level2]) - level2 = tm.makeStringIndex(N // 1000).values + level2 = Index([f"i-{i}" for i in range(N // 1000)], dtype=object).values str_left = MultiIndex.from_product([level1, level2]) data = { @@ -354,7 +357,7 @@ def setup(self, dtype): level2 = range(N // 1000) int_midx = MultiIndex.from_product([level1, level2]) - level2 = tm.makeStringIndex(N // 1000).values + level2 = Index([f"i-{i}" for i in range(N // 1000)], dtype=object).values str_midx = MultiIndex.from_product([level1, level2]) data = { @@ -411,7 +414,7 @@ def setup(self, dtype): elif dtype == "int64": level2 = range(N2) elif dtype == "string": - level2 = tm.makeStringIndex(N2) + level2 = Index([f"i-{i}" for i in range(N2)], dtype=object) else: raise NotImplementedError diff --git a/asv_bench/benchmarks/reindex.py b/asv_bench/benchmarks/reindex.py index 1c5e6050db275..3d22bfce7e2b2 100644 --- a/asv_bench/benchmarks/reindex.py +++ b/asv_bench/benchmarks/reindex.py @@ -9,8 +9,6 @@ period_range, ) -from .pandas_vb_common import tm - class Reindex: def setup(self): @@ -23,8 +21,8 @@ def setup(self): ) N = 5000 K = 200 - level1 = tm.makeStringIndex(N).values.repeat(K) - level2 = np.tile(tm.makeStringIndex(K).values, N) + level1 = Index([f"i-{i}" for i in range(N)], dtype=object).values.repeat(K) + level2 = np.tile(Index([f"i-{i}" for i in range(K)], dtype=object).values, N) index = MultiIndex.from_arrays([level1, level2]) self.s = Series(np.random.randn(N * K), index=index) self.s_subset = self.s[::2] @@ -93,8 +91,8 @@ class DropDuplicates: def setup(self, inplace): N = 10000 K = 10 - key1 = tm.makeStringIndex(N).values.repeat(K) - key2 = tm.makeStringIndex(N).values.repeat(K) + key1 = Index([f"i-{i}" for i in range(N)], dtype=object).values.repeat(K) + key2 = Index([f"i-{i}" for i in range(N)], dtype=object).values.repeat(K) self.df = DataFrame( {"key1": key1, "key2": key2, "value": np.random.randn(N * K)} ) @@ -102,7 +100,9 @@ def setup(self, inplace): self.df_nan.iloc[:10000, :] = np.nan self.s = Series(np.random.randint(0, 1000, size=10000)) - self.s_str = Series(np.tile(tm.makeStringIndex(1000).values, 10)) + self.s_str = Series( + np.tile(Index([f"i-{i}" for i in range(1000)], dtype=object).values, 10) + ) N = 1000000 K = 10000 @@ -133,7 +133,7 @@ class Align: # blog "pandas escaped the zoo" def setup(self): n = 50000 - indices = tm.makeStringIndex(n) + indices = Index([f"i-{i}" for i in range(n)], dtype=object) subsample_size = 40000 self.x = Series(np.random.randn(n), indices) self.y = Series( diff --git a/asv_bench/benchmarks/series_methods.py b/asv_bench/benchmarks/series_methods.py index 79cf8f9cd2048..b021af4694d7d 100644 --- a/asv_bench/benchmarks/series_methods.py +++ b/asv_bench/benchmarks/series_methods.py @@ -10,8 +10,6 @@ date_range, ) -from .pandas_vb_common import tm - class SeriesConstructor: def setup(self): @@ -253,7 +251,7 @@ def time_mode(self, N): class Dir: def setup(self): - self.s = Series(index=tm.makeStringIndex(10000)) + self.s = Series(index=Index([f"i-{i}" for i in range(10000)], dtype=object)) def time_dir_strings(self): dir(self.s) diff --git a/asv_bench/benchmarks/strings.py b/asv_bench/benchmarks/strings.py index 6682a60f42997..1f4a104255057 100644 --- a/asv_bench/benchmarks/strings.py +++ b/asv_bench/benchmarks/strings.py @@ -6,12 +6,11 @@ NA, Categorical, DataFrame, + Index, Series, ) from pandas.arrays import StringArray -from .pandas_vb_common import tm - class Dtypes: params = ["str", "string[python]", "string[pyarrow]"] @@ -19,7 +18,9 @@ class Dtypes: def setup(self, dtype): try: - self.s = Series(tm.makeStringIndex(10**5), dtype=dtype) + self.s = Series( + Index([f"i-{i}" for i in range(10000)], dtype=object), dtype=dtype + ) except ImportError: raise NotImplementedError @@ -172,7 +173,7 @@ class Repeat: def setup(self, repeats): N = 10**5 - self.s = Series(tm.makeStringIndex(N)) + self.s = Series(Index([f"i-{i}" for i in range(N)], dtype=object)) repeat = {"int": 1, "array": np.random.randint(1, 3, N)} self.values = repeat[repeats] @@ -187,13 +188,20 @@ class Cat: def setup(self, other_cols, sep, na_rep, na_frac): N = 10**5 mask_gen = lambda: np.random.choice([True, False], N, p=[1 - na_frac, na_frac]) - self.s = Series(tm.makeStringIndex(N)).where(mask_gen()) + self.s = Series(Index([f"i-{i}" for i in range(N)], dtype=object)).where( + mask_gen() + ) if other_cols == 0: # str.cat self-concatenates only for others=None self.others = None else: self.others = DataFrame( - {i: tm.makeStringIndex(N).where(mask_gen()) for i in range(other_cols)} + { + i: Index([f"i-{i}" for i in range(N)], dtype=object).where( + mask_gen() + ) + for i in range(other_cols) + } ) def time_cat(self, other_cols, sep, na_rep, na_frac): @@ -254,7 +262,7 @@ def time_get_dummies(self, dtype): class Encode: def setup(self): - self.ser = Series(tm.makeStringIndex()) + self.ser = Series(Index([f"i-{i}" for i in range(10_000)], dtype=object)) def time_encode_decode(self): self.ser.str.encode("utf-8").str.decode("utf-8") diff --git a/pandas/_testing/__init__.py b/pandas/_testing/__init__.py index a74fb2bf48bc4..c73d869b6c39c 100644 --- a/pandas/_testing/__init__.py +++ b/pandas/_testing/__init__.py @@ -370,11 +370,6 @@ def getCols(k) -> str: return string.ascii_uppercase[:k] -# make index -def makeStringIndex(k: int = 10, name=None) -> Index: - return Index(rands_array(nchars=10, size=k), name=name) - - def makeCategoricalIndex( k: int = 10, n: int = 3, name=None, **kwargs ) -> CategoricalIndex: @@ -385,14 +380,6 @@ def makeCategoricalIndex( ) -def makeBoolIndex(k: int = 10, name=None) -> Index: - if k == 1: - return Index([True], name=name) - elif k == 2: - return Index([False, True], name=name) - return Index([False, True] + [False] * (k - 2), name=name) - - def makeNumericIndex(k: int = 10, *, name=None, dtype: Dtype | None) -> Index: dtype = pandas_dtype(dtype) assert isinstance(dtype, np.dtype) @@ -457,14 +444,13 @@ def makePeriodIndex(k: int = 10, name=None, **kwargs) -> PeriodIndex: def makeObjectSeries(name=None) -> Series: - data = makeStringIndex(_N) - data = Index(data, dtype=object) - index = makeStringIndex(_N) - return Series(data, index=index, name=name) + data = [f"foo_{i}" for i in range(_N)] + index = Index([f"bar_{i}" for i in range(_N)]) + return Series(data, index=index, name=name, dtype=object) def getSeriesData() -> dict[str, Series]: - index = makeStringIndex(_N) + index = Index([f"foo_{i}" for i in range(_N)]) return { c: Series(np.random.default_rng(i).standard_normal(_N), index=index) for i, c in enumerate(getCols(_K)) @@ -566,7 +552,7 @@ def makeCustomIndex( idx_func_dict: dict[str, Callable[..., Index]] = { "i": makeIntIndex, "f": makeFloatIndex, - "s": makeStringIndex, + "s": lambda n: Index([f"{i}_{chr(i)}" for i in range(97, 97 + n)]), "dt": makeDateIndex, "td": makeTimedeltaIndex, "p": makePeriodIndex, @@ -1049,7 +1035,6 @@ def shares_memory(left, right) -> bool: "iat", "iloc", "loc", - "makeBoolIndex", "makeCategoricalIndex", "makeCustomDataframe", "makeCustomIndex", @@ -1062,7 +1047,6 @@ def shares_memory(left, right) -> bool: "makeObjectSeries", "makePeriodIndex", "makeRangeIndex", - "makeStringIndex", "makeTimeDataFrame", "makeTimedeltaIndex", "makeTimeSeries", diff --git a/pandas/conftest.py b/pandas/conftest.py index 350871c3085c1..3205b6657439f 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -610,7 +610,7 @@ def _create_mi_with_dt64tz_level(): indices_dict = { - "string": tm.makeStringIndex(100), + "string": Index([f"pandas_{i}" for i in range(100)]), "datetime": tm.makeDateIndex(100), "datetime-tz": tm.makeDateIndex(100, tz="US/Pacific"), "period": tm.makePeriodIndex(100), @@ -626,7 +626,7 @@ def _create_mi_with_dt64tz_level(): "uint64": tm.makeUIntIndex(100, dtype="uint64"), "float32": tm.makeFloatIndex(100, dtype="float32"), "float64": tm.makeFloatIndex(100, dtype="float64"), - "bool-object": tm.makeBoolIndex(10).astype(object), + "bool-object": Index([True, False] * 5, dtype=object), "bool-dtype": Index(np.random.default_rng(2).standard_normal(10) < 0), "complex64": tm.makeNumericIndex(100, dtype="float64").astype("complex64"), "complex128": tm.makeNumericIndex(100, dtype="float64").astype("complex128"), @@ -641,10 +641,12 @@ def _create_mi_with_dt64tz_level(): "nullable_uint": Index(np.arange(100), dtype="UInt16"), "nullable_float": Index(np.arange(100), dtype="Float32"), "nullable_bool": Index(np.arange(100).astype(bool), dtype="boolean"), - "string-python": Index(pd.array(tm.makeStringIndex(100), dtype="string[python]")), + "string-python": Index( + pd.array([f"pandas_{i}" for i in range(100)], dtype="string[python]") + ), } if has_pyarrow: - idx = Index(pd.array(tm.makeStringIndex(100), dtype="string[pyarrow]")) + idx = Index(pd.array([f"pandas_{i}" for i in range(100)], dtype="string[pyarrow]")) indices_dict["string-pyarrow"] = idx diff --git a/pandas/tests/arithmetic/test_numeric.py b/pandas/tests/arithmetic/test_numeric.py index f89711c0edee7..c3c4a8b4fc6c0 100644 --- a/pandas/tests/arithmetic/test_numeric.py +++ b/pandas/tests/arithmetic/test_numeric.py @@ -916,7 +916,7 @@ def test_add_frames(self, first, second, expected): # TODO: This came from series.test.test_operators, needs cleanup def test_series_frame_radd_bug(self, fixed_now_ts): # GH#353 - vals = Series(tm.makeStringIndex()) + vals = Series([str(i) for i in range(5)]) result = "foo_" + vals expected = vals.map(lambda x: "foo_" + x) tm.assert_series_equal(result, expected) diff --git a/pandas/tests/arithmetic/test_object.py b/pandas/tests/arithmetic/test_object.py index 33953900c2006..6b36f447eb7d5 100644 --- a/pandas/tests/arithmetic/test_object.py +++ b/pandas/tests/arithmetic/test_object.py @@ -299,7 +299,7 @@ def test_iadd_string(self): @pytest.mark.xfail(using_pyarrow_string_dtype(), reason="add doesn't work") def test_add(self): - index = tm.makeStringIndex(100) + index = pd.Index([str(i) for i in range(10)]) expected = pd.Index(index.values * 2) tm.assert_index_equal(index + index, expected) tm.assert_index_equal(index + index.tolist(), expected) @@ -313,7 +313,7 @@ def test_add(self): tm.assert_index_equal("1" + index, expected) def test_sub_fail(self, using_infer_string): - index = tm.makeStringIndex(100) + index = pd.Index([str(i) for i in range(10)]) if using_infer_string: import pyarrow as pa diff --git a/pandas/tests/frame/methods/test_first_valid_index.py b/pandas/tests/frame/methods/test_first_valid_index.py index a448768f4173d..2e27f1aa71700 100644 --- a/pandas/tests/frame/methods/test_first_valid_index.py +++ b/pandas/tests/frame/methods/test_first_valid_index.py @@ -6,9 +6,10 @@ from pandas import ( DataFrame, + Index, Series, + date_range, ) -import pandas._testing as tm class TestFirstValidIndex: @@ -44,11 +45,12 @@ def test_first_last_valid_frame(self, data, idx, expected_first, expected_last): assert expected_first == df.first_valid_index() assert expected_last == df.last_valid_index() - @pytest.mark.parametrize("index_func", [tm.makeStringIndex, tm.makeDateIndex]) - def test_first_last_valid(self, index_func): - N = 30 - index = index_func(N) - mat = np.random.default_rng(2).standard_normal(N) + @pytest.mark.parametrize( + "index", + [Index([str(i) for i in range(20)]), date_range("2020-01-01", periods=20)], + ) + def test_first_last_valid(self, index): + mat = np.random.default_rng(2).standard_normal(len(index)) mat[:5] = np.nan mat[-5:] = np.nan @@ -60,10 +62,12 @@ def test_first_last_valid(self, index_func): assert ser.first_valid_index() == frame.index[5] assert ser.last_valid_index() == frame.index[-6] - @pytest.mark.parametrize("index_func", [tm.makeStringIndex, tm.makeDateIndex]) - def test_first_last_valid_all_nan(self, index_func): + @pytest.mark.parametrize( + "index", + [Index([str(i) for i in range(10)]), date_range("2020-01-01", periods=10)], + ) + def test_first_last_valid_all_nan(self, index): # GH#17400: no valid entries - index = index_func(30) frame = DataFrame(np.nan, columns=["foo"], index=index) assert frame.last_valid_index() is None diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index bf17b61b0e3f3..3111075c5c1a7 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -784,7 +784,7 @@ def test_constructor_dict_cast(self): def test_constructor_dict_cast2(self): # can't cast to float test_data = { - "A": dict(zip(range(20), tm.makeStringIndex(20))), + "A": dict(zip(range(20), [f"word_{i}" for i in range(20)])), "B": dict(zip(range(15), np.random.default_rng(2).standard_normal(15))), } with pytest.raises(ValueError, match="could not convert string"): diff --git a/pandas/tests/frame/test_repr.py b/pandas/tests/frame/test_repr.py index 98962b3003b6d..eed48b9db116b 100644 --- a/pandas/tests/frame/test_repr.py +++ b/pandas/tests/frame/test_repr.py @@ -165,7 +165,7 @@ def test_repr_mixed_big(self): biggie = DataFrame( { "A": np.random.default_rng(2).standard_normal(200), - "B": tm.makeStringIndex(200), + "B": [str(i) for i in range(200)], }, index=range(200), ) diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py index 3c1a35c984031..c401762dace23 100644 --- a/pandas/tests/groupby/test_grouping.py +++ b/pandas/tests/groupby/test_grouping.py @@ -19,6 +19,7 @@ Series, Timestamp, date_range, + period_range, ) import pandas._testing as tm from pandas.core.groupby.grouper import Grouping @@ -174,23 +175,21 @@ class TestGrouping: @pytest.mark.parametrize( "index", [ - tm.makeFloatIndex, - tm.makeStringIndex, - tm.makeIntIndex, - tm.makeDateIndex, - tm.makePeriodIndex, + Index(list("abcde")), + Index(np.arange(5)), + Index(np.arange(5, dtype=float)), + date_range("2020-01-01", periods=5), + period_range("2020-01-01", periods=5), ], ) - @pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_grouper_index_types(self, index): # related GH5375 # groupby misbehaving when using a Floatlike index - df = DataFrame(np.arange(10).reshape(5, 2), columns=list("AB")) + df = DataFrame(np.arange(10).reshape(5, 2), columns=list("AB"), index=index) - df.index = index(len(df)) df.groupby(list("abcde"), group_keys=False).apply(lambda x: x) - df.index = list(reversed(df.index.tolist())) + df.index = df.index[::-1] df.groupby(list("abcde"), group_keys=False).apply(lambda x: x) def test_grouper_multilevel_freq(self): diff --git a/pandas/tests/indexes/multi/test_duplicates.py b/pandas/tests/indexes/multi/test_duplicates.py index a69248cf038f8..6c6d9022b1af3 100644 --- a/pandas/tests/indexes/multi/test_duplicates.py +++ b/pandas/tests/indexes/multi/test_duplicates.py @@ -257,7 +257,7 @@ def test_duplicated(idx_dup, keep, expected): def test_duplicated_hashtable_impl(keep, monkeypatch): # GH 9125 n, k = 6, 10 - levels = [np.arange(n), tm.makeStringIndex(n), 1000 + np.arange(n)] + levels = [np.arange(n), [str(i) for i in range(n)], 1000 + np.arange(n)] codes = [np.random.default_rng(2).choice(n, k * n) for _ in levels] with monkeypatch.context() as m: m.setattr(libindex, "_SIZE_CUTOFF", 50) diff --git a/pandas/tests/indexing/test_floats.py b/pandas/tests/indexing/test_floats.py index c9fbf95751dfe..cf9966145afce 100644 --- a/pandas/tests/indexing/test_floats.py +++ b/pandas/tests/indexing/test_floats.py @@ -6,6 +6,9 @@ Index, RangeIndex, Series, + date_range, + period_range, + timedelta_range, ) import pandas._testing as tm @@ -39,22 +42,21 @@ def check(self, result, original, indexer, getitem): tm.assert_almost_equal(result, expected) @pytest.mark.parametrize( - "index_func", + "index", [ - tm.makeStringIndex, - tm.makeCategoricalIndex, - tm.makeDateIndex, - tm.makeTimedeltaIndex, - tm.makePeriodIndex, + Index(list("abcde")), + Index(list("abcde"), dtype="category"), + date_range("2020-01-01", periods=5), + timedelta_range("1 day", periods=5), + period_range("2020-01-01", periods=5), ], ) - def test_scalar_non_numeric(self, index_func, frame_or_series, indexer_sl): + def test_scalar_non_numeric(self, index, frame_or_series, indexer_sl): # GH 4892 # float_indexers should raise exceptions # on appropriate Index types & accessors - i = index_func(5) - s = gen_obj(frame_or_series, i) + s = gen_obj(frame_or_series, index) # getting with pytest.raises(KeyError, match="^3.0$"): @@ -75,19 +77,18 @@ def test_scalar_non_numeric(self, index_func, frame_or_series, indexer_sl): assert 3.0 not in s2.axes[-1] @pytest.mark.parametrize( - "index_func", + "index", [ - tm.makeStringIndex, - tm.makeCategoricalIndex, - tm.makeDateIndex, - tm.makeTimedeltaIndex, - tm.makePeriodIndex, + Index(list("abcde")), + Index(list("abcde"), dtype="category"), + date_range("2020-01-01", periods=5), + timedelta_range("1 day", periods=5), + period_range("2020-01-01", periods=5), ], ) - def test_scalar_non_numeric_series_fallback(self, index_func): + def test_scalar_non_numeric_series_fallback(self, index): # fallsback to position selection, series only - i = index_func(5) - s = Series(np.arange(len(i)), index=i) + s = Series(np.arange(len(index)), index=index) msg = "Series.__getitem__ treating keys as positions is deprecated" with tm.assert_produces_warning(FutureWarning, match=msg): @@ -214,21 +215,20 @@ def test_scalar_float(self, frame_or_series): self.check(result, s, 3, False) @pytest.mark.parametrize( - "index_func", + "index", [ - tm.makeStringIndex, - tm.makeDateIndex, - tm.makeTimedeltaIndex, - tm.makePeriodIndex, + Index(list("abcde"), dtype=object), + date_range("2020-01-01", periods=5), + timedelta_range("1 day", periods=5), + period_range("2020-01-01", periods=5), ], ) @pytest.mark.parametrize("idx", [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)]) - def test_slice_non_numeric(self, index_func, idx, frame_or_series, indexer_sli): + def test_slice_non_numeric(self, index, idx, frame_or_series, indexer_sli): # GH 4892 # float_indexers should raise exceptions # on appropriate Index types & accessors - index = index_func(5) s = gen_obj(frame_or_series, index) # getitem diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index edac82193d1c8..f5738b83a8e64 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -25,7 +25,6 @@ read_csv, reset_option, ) -import pandas._testing as tm from pandas.io.formats import printing import pandas.io.formats.format as fmt @@ -834,19 +833,21 @@ def test_to_string_buffer_all_unicode(self): buf.getvalue() @pytest.mark.parametrize( - "index", + "index_scalar", [ - tm.makeStringIndex, - tm.makeIntIndex, - tm.makeDateIndex, - tm.makePeriodIndex, + "a" * 10, + 1, + Timestamp(2020, 1, 1), + pd.Period("2020-01-01"), ], ) @pytest.mark.parametrize("h", [10, 20]) @pytest.mark.parametrize("w", [10, 20]) - def test_to_string_truncate_indices(self, index, h, w): + def test_to_string_truncate_indices(self, index_scalar, h, w): with option_context("display.expand_frame_repr", False): - df = DataFrame(index=index(h), columns=tm.makeStringIndex(w)) + df = DataFrame( + index=[index_scalar] * h, columns=[str(i) * 10 for i in range(w)] + ) with option_context("display.max_rows", 15): if h == 20: assert has_vertically_truncated_repr(df) diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index a7648cf1c471a..605e5e182d8cc 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -59,7 +59,7 @@ def biggie_df_fixture(request): df = DataFrame( { "A": np.random.default_rng(2).standard_normal(200), - "B": tm.makeStringIndex(200), + "B": Index([f"{i}?!" for i in range(200)]), }, index=np.arange(200), ) diff --git a/pandas/tests/io/formats/test_to_string.py b/pandas/tests/io/formats/test_to_string.py index e607b6eb454a1..02c20b0e25477 100644 --- a/pandas/tests/io/formats/test_to_string.py +++ b/pandas/tests/io/formats/test_to_string.py @@ -804,7 +804,7 @@ def test_to_string(self): biggie = DataFrame( { "A": np.random.default_rng(2).standard_normal(200), - "B": tm.makeStringIndex(200), + "B": Index([f"{i}?!" for i in range(200)]), }, ) diff --git a/pandas/tests/io/pytables/test_put.py b/pandas/tests/io/pytables/test_put.py index c109b72e9c239..59a05dc9ea546 100644 --- a/pandas/tests/io/pytables/test_put.py +++ b/pandas/tests/io/pytables/test_put.py @@ -196,32 +196,27 @@ def test_put_mixed_type(setup_path): tm.assert_frame_equal(expected, df) +@pytest.mark.parametrize("format", ["table", "fixed"]) @pytest.mark.parametrize( - "format, index", + "index", [ - ["table", tm.makeFloatIndex], - ["table", tm.makeStringIndex], - ["table", tm.makeIntIndex], - ["table", tm.makeDateIndex], - ["fixed", tm.makeFloatIndex], - ["fixed", tm.makeStringIndex], - ["fixed", tm.makeIntIndex], - ["fixed", tm.makeDateIndex], - ["table", tm.makePeriodIndex], # GH#7796 - ["fixed", tm.makePeriodIndex], + Index([str(i) for i in range(10)]), + Index(np.arange(10, dtype=float)), + Index(np.arange(10)), + date_range("2020-01-01", periods=10), + pd.period_range("2020-01-01", periods=10), ], ) -@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning") def test_store_index_types(setup_path, format, index): # GH5386 # test storing various index types with ensure_clean_store(setup_path) as store: df = DataFrame( - np.random.default_rng(2).standard_normal((10, 2)), columns=list("AB") + np.random.default_rng(2).standard_normal((10, 2)), + columns=list("AB"), + index=index, ) - df.index = index(len(df)) - _maybe_remove(store, "df") store.put("df", df, format=format) tm.assert_frame_equal(df, store["df"]) diff --git a/pandas/tests/io/pytables/test_round_trip.py b/pandas/tests/io/pytables/test_round_trip.py index 6c24843f18d0d..d06935871cb56 100644 --- a/pandas/tests/io/pytables/test_round_trip.py +++ b/pandas/tests/io/pytables/test_round_trip.py @@ -51,7 +51,8 @@ def roundtrip(key, obj, **kwargs): def test_long_strings(setup_path): # GH6166 - df = DataFrame({"a": tm.makeStringIndex(10)}, index=tm.makeStringIndex(10)) + data = ["a" * 50] * 10 + df = DataFrame({"a": data}, index=data) with ensure_clean_store(setup_path) as store: store.append("df", df, data_columns=["a"]) @@ -65,7 +66,7 @@ def test_api(tmp_path, setup_path): # API issue when to_hdf doesn't accept append AND format args path = tmp_path / setup_path - df = tm.makeDataFrame() + df = DataFrame(range(20)) df.iloc[:10].to_hdf(path, key="df", append=True, format="table") df.iloc[10:].to_hdf(path, key="df", append=True, format="table") tm.assert_frame_equal(read_hdf(path, "df"), df) @@ -79,7 +80,7 @@ def test_api(tmp_path, setup_path): def test_api_append(tmp_path, setup_path): path = tmp_path / setup_path - df = tm.makeDataFrame() + df = DataFrame(range(20)) df.iloc[:10].to_hdf(path, key="df", append=True) df.iloc[10:].to_hdf(path, key="df", append=True, format="table") tm.assert_frame_equal(read_hdf(path, "df"), df) @@ -93,7 +94,7 @@ def test_api_append(tmp_path, setup_path): def test_api_2(tmp_path, setup_path): path = tmp_path / setup_path - df = tm.makeDataFrame() + df = DataFrame(range(20)) df.to_hdf(path, key="df", append=False, format="fixed") tm.assert_frame_equal(read_hdf(path, "df"), df) @@ -107,7 +108,7 @@ def test_api_2(tmp_path, setup_path): tm.assert_frame_equal(read_hdf(path, "df"), df) with ensure_clean_store(setup_path) as store: - df = tm.makeDataFrame() + df = DataFrame(range(20)) _maybe_remove(store, "df") store.append("df", df.iloc[:10], append=True, format="table") diff --git a/pandas/tests/io/pytables/test_store.py b/pandas/tests/io/pytables/test_store.py index 96c160ab40bd8..df8a1e3cb7470 100644 --- a/pandas/tests/io/pytables/test_store.py +++ b/pandas/tests/io/pytables/test_store.py @@ -956,10 +956,6 @@ def test_to_hdf_with_object_column_names(tmp_path, setup_path): tm.makeTimedeltaIndex, tm.makePeriodIndex, ] - types_should_run = [ - tm.makeStringIndex, - tm.makeCategoricalIndex, - ] for index in types_should_fail: df = DataFrame( @@ -970,14 +966,18 @@ def test_to_hdf_with_object_column_names(tmp_path, setup_path): with pytest.raises(ValueError, match=msg): df.to_hdf(path, key="df", format="table", data_columns=True) - for index in types_should_run: - df = DataFrame( - np.random.default_rng(2).standard_normal((10, 2)), columns=index(2) - ) - path = tmp_path / setup_path - df.to_hdf(path, key="df", format="table", data_columns=True) - result = read_hdf(path, "df", where=f"index = [{df.index[0]}]") - assert len(result) + +@pytest.mark.parametrize("dtype", [None, "category"]) +def test_to_hdf_with_object_column_names_should_run(tmp_path, setup_path, dtype): + # GH9057 + df = DataFrame( + np.random.default_rng(2).standard_normal((10, 2)), + columns=Index(["a", "b"], dtype=dtype), + ) + path = tmp_path / setup_path + df.to_hdf(path, key="df", format="table", data_columns=True) + result = read_hdf(path, "df", where=f"index = [{df.index[0]}]") + assert len(result) def test_hdfstore_strides(setup_path): diff --git a/pandas/tests/reductions/test_reductions.py b/pandas/tests/reductions/test_reductions.py index 9c4ae92224148..303f8550c5a80 100644 --- a/pandas/tests/reductions/test_reductions.py +++ b/pandas/tests/reductions/test_reductions.py @@ -33,13 +33,13 @@ def get_objs(): indexes = [ - tm.makeBoolIndex(10, name="a"), + Index([True, False] * 5, name="a"), tm.makeIntIndex(10, name="a"), tm.makeFloatIndex(10, name="a"), tm.makeDateIndex(10, name="a"), tm.makeDateIndex(10, name="a").tz_localize(tz="US/Eastern"), tm.makePeriodIndex(10, name="a"), - tm.makeStringIndex(10, name="a"), + Index([str(i) for i in range(10)], name="a"), ] arr = np.random.default_rng(2).standard_normal(10) diff --git a/pandas/tests/resample/test_time_grouper.py b/pandas/tests/resample/test_time_grouper.py index 1dc25cb9d4c1e..3d9098917a12d 100644 --- a/pandas/tests/resample/test_time_grouper.py +++ b/pandas/tests/resample/test_time_grouper.py @@ -87,19 +87,17 @@ def f(df): @pytest.mark.parametrize( - "func", + "index", [ - tm.makeIntIndex, - tm.makeStringIndex, - tm.makeFloatIndex, - (lambda m: tm.makeCustomIndex(m, 2)), + Index([1, 2]), + Index(["a", "b"]), + Index([1.1, 2.2]), + pd.MultiIndex.from_arrays([[1, 2], ["a", "b"]]), ], ) -def test_fails_on_no_datetime_index(func): - n = 2 - index = func(n) +def test_fails_on_no_datetime_index(index): name = type(index).__name__ - df = DataFrame({"a": np.random.default_rng(2).standard_normal(n)}, index=index) + df = DataFrame({"a": range(len(index))}, index=index) msg = ( "Only valid with DatetimeIndex, TimedeltaIndex " diff --git a/pandas/tests/reshape/merge/test_multi.py b/pandas/tests/reshape/merge/test_multi.py index f5d78fbd44812..269d3a2b7078e 100644 --- a/pandas/tests/reshape/merge/test_multi.py +++ b/pandas/tests/reshape/merge/test_multi.py @@ -188,7 +188,7 @@ def test_merge_multiple_cols_with_mixed_cols_index(self): def test_compress_group_combinations(self): # ~ 40000000 possible unique groups - key1 = tm.makeStringIndex(10000) + key1 = [str(i) for i in range(10000)] key1 = np.tile(key1, 2) key2 = key1[::-1] diff --git a/pandas/tests/series/methods/test_combine_first.py b/pandas/tests/series/methods/test_combine_first.py index 795b2eab82aca..1cbb7c7982802 100644 --- a/pandas/tests/series/methods/test_combine_first.py +++ b/pandas/tests/series/methods/test_combine_first.py @@ -51,9 +51,9 @@ def test_combine_first(self): tm.assert_series_equal(combined[1::2], series_copy[1::2]) # mixed types - index = tm.makeStringIndex(20) + index = pd.Index([str(i) for i in range(20)]) floats = Series(np.random.default_rng(2).standard_normal(20), index=index) - strings = Series(tm.makeStringIndex(10), index=index[::2], dtype=object) + strings = Series([str(i) for i in range(10)], index=index[::2], dtype=object) combined = strings.combine_first(floats) diff --git a/pandas/tests/series/test_api.py b/pandas/tests/series/test_api.py index a39b3ff7e6f2b..c29fe6ba06ab4 100644 --- a/pandas/tests/series/test_api.py +++ b/pandas/tests/series/test_api.py @@ -68,7 +68,7 @@ def test_tab_completion_with_categorical(self): @pytest.mark.parametrize( "index", [ - tm.makeStringIndex(10), + Index([str(i) for i in range(10)]), tm.makeCategoricalIndex(10), Index(["foo", "bar", "baz"] * 2), tm.makeDateIndex(10), diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index a6e63dfd5f409..24c4706810154 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -1663,19 +1663,18 @@ def test_unique_complex_numbers(self, array, expected): class TestHashTable: @pytest.mark.parametrize( - "htable, tm_dtype", + "htable, data", [ - (ht.PyObjectHashTable, "String"), - (ht.StringHashTable, "String"), - (ht.Float64HashTable, "Float"), - (ht.Int64HashTable, "Int"), - (ht.UInt64HashTable, "UInt"), + (ht.PyObjectHashTable, [f"foo_{i}" for i in range(1000)]), + (ht.StringHashTable, [f"foo_{i}" for i in range(1000)]), + (ht.Float64HashTable, np.arange(1000, dtype=np.float64)), + (ht.Int64HashTable, np.arange(1000, dtype=np.int64)), + (ht.UInt64HashTable, np.arange(1000, dtype=np.uint64)), ], ) - def test_hashtable_unique(self, htable, tm_dtype, writable): + def test_hashtable_unique(self, htable, data, writable): # output of maker has guaranteed unique elements - maker = getattr(tm, "make" + tm_dtype + "Index") - s = Series(maker(1000)) + s = Series(data) if htable == ht.Float64HashTable: # add NaN for float column s.loc[500] = np.nan @@ -1703,19 +1702,18 @@ def test_hashtable_unique(self, htable, tm_dtype, writable): tm.assert_numpy_array_equal(reconstr, s_duplicated.values) @pytest.mark.parametrize( - "htable, tm_dtype", + "htable, data", [ - (ht.PyObjectHashTable, "String"), - (ht.StringHashTable, "String"), - (ht.Float64HashTable, "Float"), - (ht.Int64HashTable, "Int"), - (ht.UInt64HashTable, "UInt"), + (ht.PyObjectHashTable, [f"foo_{i}" for i in range(1000)]), + (ht.StringHashTable, [f"foo_{i}" for i in range(1000)]), + (ht.Float64HashTable, np.arange(1000, dtype=np.float64)), + (ht.Int64HashTable, np.arange(1000, dtype=np.int64)), + (ht.UInt64HashTable, np.arange(1000, dtype=np.uint64)), ], ) - def test_hashtable_factorize(self, htable, tm_dtype, writable): + def test_hashtable_factorize(self, htable, writable, data): # output of maker has guaranteed unique elements - maker = getattr(tm, "make" + tm_dtype + "Index") - s = Series(maker(1000)) + s = Series(data) if htable == ht.Float64HashTable: # add NaN for float column s.loc[500] = np.nan diff --git a/pandas/tests/tseries/frequencies/test_inference.py b/pandas/tests/tseries/frequencies/test_inference.py index 75528a8b99c4d..5d22896d9d055 100644 --- a/pandas/tests/tseries/frequencies/test_inference.py +++ b/pandas/tests/tseries/frequencies/test_inference.py @@ -401,7 +401,7 @@ def test_invalid_index_types_unicode(): msg = "Unknown datetime string format" with pytest.raises(ValueError, match=msg): - frequencies.infer_freq(tm.makeStringIndex(10)) + frequencies.infer_freq(Index(["ZqgszYBfuL"])) def test_string_datetime_like_compat(): diff --git a/pandas/tests/util/test_hashing.py b/pandas/tests/util/test_hashing.py index 00dc184a0ac4d..e7c4c27714d5f 100644 --- a/pandas/tests/util/test_hashing.py +++ b/pandas/tests/util/test_hashing.py @@ -328,9 +328,9 @@ def test_alternate_encoding(index): @pytest.mark.parametrize("l_add", [0, 1]) def test_same_len_hash_collisions(l_exp, l_add): length = 2 ** (l_exp + 8) + l_add - s = tm.makeStringIndex(length).to_numpy() + idx = np.array([str(i) for i in range(length)], dtype=object) - result = hash_array(s, "utf8") + result = hash_array(idx, "utf8") assert not result[0] == result[1]