Skip to content

Commit de55c3d

Browse files
authored
STYLE use ast.parse instead of regex to fixup inconsistencies in namespace (#39690)
1 parent aa58447 commit de55c3d

16 files changed

+167
-104
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ repos:
138138
entry: python scripts/check_for_inconsistent_pandas_namespace.py
139139
language: python
140140
types: [python]
141-
files: ^pandas/tests/
141+
files: ^pandas/tests/frame/
142142
- id: FrameOrSeriesUnion
143143
name: Check for use of Union[Series, DataFrame] instead of FrameOrSeriesUnion alias
144144
entry: Union\[.*(Series,.*DataFrame|DataFrame,.*Series).*\]

pandas/tests/frame/indexing/test_indexing.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def test_setitem_multi_index(self):
217217
it = ["jim", "joe", "jolie"], ["first", "last"], ["left", "center", "right"]
218218

219219
cols = MultiIndex.from_product(it)
220-
index = pd.date_range("20141006", periods=20)
220+
index = date_range("20141006", periods=20)
221221
vals = np.random.randint(1, 1000, (len(index), len(cols)))
222222
df = DataFrame(vals, columns=cols, index=index)
223223

@@ -1357,7 +1357,7 @@ def test_loc_duplicates(self):
13571357
# gh-17105
13581358

13591359
# insert a duplicate element to the index
1360-
trange = pd.date_range(
1360+
trange = date_range(
13611361
start=Timestamp(year=2017, month=1, day=1),
13621362
end=Timestamp(year=2017, month=1, day=5),
13631363
)
@@ -1421,7 +1421,7 @@ def test_setitem_with_unaligned_tz_aware_datetime_column(self):
14211421
# GH 12981
14221422
# Assignment of unaligned offset-aware datetime series.
14231423
# Make sure timezone isn't lost
1424-
column = Series(pd.date_range("2015-01-01", periods=3, tz="utc"), name="dates")
1424+
column = Series(date_range("2015-01-01", periods=3, tz="utc"), name="dates")
14251425
df = DataFrame({"dates": column})
14261426
df["dates"] = column[[1, 0, 2]]
14271427
tm.assert_series_equal(df["dates"], column)
@@ -1716,7 +1716,7 @@ def test_object_casting_indexing_wraps_datetimelike():
17161716
df = DataFrame(
17171717
{
17181718
"A": [1, 2],
1719-
"B": pd.date_range("2000", periods=2),
1719+
"B": date_range("2000", periods=2),
17201720
"C": pd.timedelta_range("1 Day", periods=2),
17211721
}
17221722
)

pandas/tests/frame/methods/test_describe.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def test_describe_tz_values(self, tz_naive_fixture):
283283
tm.assert_frame_equal(result, expected)
284284

285285
def test_datetime_is_numeric_includes_datetime(self):
286-
df = DataFrame({"a": pd.date_range("2012", periods=3), "b": [1, 2, 3]})
286+
df = DataFrame({"a": date_range("2012", periods=3), "b": [1, 2, 3]})
287287
result = df.describe(datetime_is_numeric=True)
288288
expected = DataFrame(
289289
{

pandas/tests/frame/methods/test_diff.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def test_diff_datetime_axis0_with_nat(self, tz):
8080
@pytest.mark.parametrize("tz", [None, "UTC"])
8181
def test_diff_datetime_with_nat_zero_periods(self, tz):
8282
# diff on NaT values should give NaT, not timedelta64(0)
83-
dti = pd.date_range("2016-01-01", periods=4, tz=tz)
83+
dti = date_range("2016-01-01", periods=4, tz=tz)
8484
ser = Series(dti)
8585
df = ser.to_frame()
8686

@@ -178,7 +178,7 @@ def test_diff_axis(self):
178178

179179
def test_diff_period(self):
180180
# GH#32995 Don't pass an incorrect axis
181-
pi = pd.date_range("2016-01-01", periods=3).to_period("D")
181+
pi = date_range("2016-01-01", periods=3).to_period("D")
182182
df = DataFrame({"A": pi})
183183

184184
result = df.diff(1, axis=1)

pandas/tests/frame/methods/test_drop.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
def test_drop_raise_exception_if_labels_not_in_level(msg, labels, level):
2828
# GH 8594
2929
mi = MultiIndex.from_arrays([[1, 2, 3], [4, 5, 6]], names=["a", "b"])
30-
s = pd.Series([10, 20, 30], index=mi)
30+
s = Series([10, 20, 30], index=mi)
3131
df = DataFrame([10, 20, 30], index=mi)
3232

3333
with pytest.raises(KeyError, match=msg):
@@ -40,7 +40,7 @@ def test_drop_raise_exception_if_labels_not_in_level(msg, labels, level):
4040
def test_drop_errors_ignore(labels, level):
4141
# GH 8594
4242
mi = MultiIndex.from_arrays([[1, 2, 3], [4, 5, 6]], names=["a", "b"])
43-
s = pd.Series([10, 20, 30], index=mi)
43+
s = Series([10, 20, 30], index=mi)
4444
df = DataFrame([10, 20, 30], index=mi)
4545

4646
expected_s = s.drop(labels, level=level, errors="ignore")

pandas/tests/frame/methods/test_join.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ def test_join_multiindex_leftright(self):
310310
tm.assert_frame_equal(df1.join(df2, how="left"), exp)
311311
tm.assert_frame_equal(df2.join(df1, how="right"), exp[["value2", "value1"]])
312312

313-
exp_idx = pd.MultiIndex.from_product(
313+
exp_idx = MultiIndex.from_product(
314314
[["a", "b"], ["x", "y", "z"]], names=["first", "second"]
315315
)
316316
exp = DataFrame(

pandas/tests/frame/methods/test_reset_index.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ def test_reset_index_multiindex_columns(self):
426426
def test_reset_index_datetime(self, tz_naive_fixture):
427427
# GH#3950
428428
tz = tz_naive_fixture
429-
idx1 = pd.date_range("1/1/2011", periods=5, freq="D", tz=tz, name="idx1")
429+
idx1 = date_range("1/1/2011", periods=5, freq="D", tz=tz, name="idx1")
430430
idx2 = Index(range(5), name="idx2", dtype="int64")
431431
idx = MultiIndex.from_arrays([idx1, idx2])
432432
df = DataFrame(
@@ -453,7 +453,7 @@ def test_reset_index_datetime(self, tz_naive_fixture):
453453

454454
tm.assert_frame_equal(df.reset_index(), expected)
455455

456-
idx3 = pd.date_range(
456+
idx3 = date_range(
457457
"1/1/2012", periods=5, freq="MS", tz="Europe/Paris", name="idx3"
458458
)
459459
idx = MultiIndex.from_arrays([idx1, idx2, idx3])
@@ -492,7 +492,7 @@ def test_reset_index_datetime(self, tz_naive_fixture):
492492

493493
# GH#7793
494494
idx = MultiIndex.from_product(
495-
[["a", "b"], pd.date_range("20130101", periods=3, tz=tz)]
495+
[["a", "b"], date_range("20130101", periods=3, tz=tz)]
496496
)
497497
df = DataFrame(
498498
np.arange(6, dtype="int64").reshape(6, 1), columns=["a"], index=idx

pandas/tests/frame/methods/test_to_csv.py

+14-19
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
DataFrame,
1313
Index,
1414
MultiIndex,
15+
NaT,
1516
Series,
1617
Timestamp,
1718
date_range,
@@ -41,7 +42,7 @@ def read_csv(self, path, **kwargs):
4142
params = {"index_col": 0, "parse_dates": True}
4243
params.update(**kwargs)
4344

44-
return pd.read_csv(path, **params)
45+
return read_csv(path, **params)
4546

4647
def test_to_csv_from_csv1(self, float_frame, datetime_frame):
4748

@@ -123,7 +124,7 @@ def test_to_csv_from_csv3(self):
123124
df1.to_csv(path)
124125
df2.to_csv(path, mode="a", header=False)
125126
xp = pd.concat([df1, df2])
126-
rs = pd.read_csv(path, index_col=0)
127+
rs = read_csv(path, index_col=0)
127128
rs.columns = [int(label) for label in rs.columns]
128129
xp.columns = [int(label) for label in xp.columns]
129130
tm.assert_frame_equal(xp, rs)
@@ -139,7 +140,7 @@ def test_to_csv_from_csv4(self):
139140
)
140141
df.to_csv(path)
141142

142-
result = pd.read_csv(path, index_col="dt_index")
143+
result = read_csv(path, index_col="dt_index")
143144
result.index = pd.to_timedelta(result.index)
144145
# TODO: remove renaming when GH 10875 is solved
145146
result.index = result.index.rename("dt_index")
@@ -153,7 +154,7 @@ def test_to_csv_from_csv5(self, timezone_frame):
153154
with tm.ensure_clean("__tmp_to_csv_from_csv5__") as path:
154155

155156
timezone_frame.to_csv(path)
156-
result = pd.read_csv(path, index_col=0, parse_dates=["A"])
157+
result = read_csv(path, index_col=0, parse_dates=["A"])
157158

158159
converter = (
159160
lambda c: to_datetime(result[c])
@@ -166,8 +167,6 @@ def test_to_csv_from_csv5(self, timezone_frame):
166167

167168
def test_to_csv_cols_reordering(self):
168169
# GH3454
169-
import pandas as pd
170-
171170
chunksize = 5
172171
N = int(chunksize * 2.5)
173172

@@ -177,17 +176,15 @@ def test_to_csv_cols_reordering(self):
177176

178177
with tm.ensure_clean() as path:
179178
df.to_csv(path, columns=cols, chunksize=chunksize)
180-
rs_c = pd.read_csv(path, index_col=0)
179+
rs_c = read_csv(path, index_col=0)
181180

182181
tm.assert_frame_equal(df[cols], rs_c, check_names=False)
183182

184183
def test_to_csv_new_dupe_cols(self):
185-
import pandas as pd
186-
187184
def _check_df(df, cols=None):
188185
with tm.ensure_clean() as path:
189186
df.to_csv(path, columns=cols, chunksize=chunksize)
190-
rs_c = pd.read_csv(path, index_col=0)
187+
rs_c = read_csv(path, index_col=0)
191188

192189
# we wrote them in a different order
193190
# so compare them in that order
@@ -227,8 +224,6 @@ def _check_df(df, cols=None):
227224
@pytest.mark.slow
228225
def test_to_csv_dtnat(self):
229226
# GH3437
230-
from pandas import NaT
231-
232227
def make_dtnat_arr(n, nnat=None):
233228
if nnat is None:
234229
nnat = int(n * 0.1) # 10%
@@ -999,7 +994,7 @@ def test_to_csv_path_is_none(self, float_frame):
999994
# Series.to_csv()
1000995
csv_str = float_frame.to_csv(path_or_buf=None)
1001996
assert isinstance(csv_str, str)
1002-
recons = pd.read_csv(StringIO(csv_str), index_col=0)
997+
recons = read_csv(StringIO(csv_str), index_col=0)
1003998
tm.assert_frame_equal(float_frame, recons)
1004999

10051000
@pytest.mark.parametrize(
@@ -1040,7 +1035,7 @@ def test_to_csv_compression(self, df, encoding, compression):
10401035
df.to_csv(handles.handle, encoding=encoding)
10411036
assert not handles.handle.closed
10421037

1043-
result = pd.read_csv(
1038+
result = read_csv(
10441039
filename,
10451040
compression=compression,
10461041
encoding=encoding,
@@ -1122,7 +1117,7 @@ def test_to_csv_with_dst_transitions(self):
11221117

11231118
with tm.ensure_clean("csv_date_format_with_dst") as path:
11241119
# make sure we are not failing on transitions
1125-
times = pd.date_range(
1120+
times = date_range(
11261121
"2013-10-26 23:00",
11271122
"2013-10-27 01:00",
11281123
tz="Europe/London",
@@ -1144,7 +1139,7 @@ def test_to_csv_with_dst_transitions(self):
11441139
tm.assert_frame_equal(result, df)
11451140

11461141
# GH11619
1147-
idx = pd.date_range("2015-01-01", "2015-12-31", freq="H", tz="Europe/Paris")
1142+
idx = date_range("2015-01-01", "2015-12-31", freq="H", tz="Europe/Paris")
11481143
idx = idx._with_freq(None) # freq does not round-trip
11491144
idx._data._freq = None # otherwise there is trouble on unpickle
11501145
df = DataFrame({"values": 1, "idx": idx}, index=idx)
@@ -1250,7 +1245,7 @@ def test_to_csv_quoting(self):
12501245
# presents with encoding?
12511246
text_rows = ["a,b,c", '1,"test \r\n",3']
12521247
text = tm.convert_rows_list_to_csv_str(text_rows)
1253-
df = pd.read_csv(StringIO(text))
1248+
df = read_csv(StringIO(text))
12541249

12551250
buf = StringIO()
12561251
df.to_csv(buf, encoding="utf-8", index=False)
@@ -1286,7 +1281,7 @@ def test_period_index_date_overflow(self):
12861281
assert result == expected
12871282

12881283
# Overflow with pd.NaT
1289-
dates = ["1990-01-01", pd.NaT, "3005-01-01"]
1284+
dates = ["1990-01-01", NaT, "3005-01-01"]
12901285
index = pd.PeriodIndex(dates, freq="D")
12911286

12921287
df = DataFrame([4, 5, 6], index=index)
@@ -1298,7 +1293,7 @@ def test_period_index_date_overflow(self):
12981293

12991294
def test_multi_index_header(self):
13001295
# see gh-5539
1301-
columns = pd.MultiIndex.from_tuples([("a", 1), ("a", 2), ("b", 1), ("b", 2)])
1296+
columns = MultiIndex.from_tuples([("a", 1), ("a", 2), ("b", 1), ("b", 2)])
13021297
df = DataFrame([[1, 2, 3, 4], [5, 6, 7, 8]])
13031298
df.columns = columns
13041299

pandas/tests/frame/test_api.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_tab_completion(self):
7373
df = DataFrame([list("abcd"), list("efgh")], columns=list("ABCD"))
7474
for key in list("ABCD"):
7575
assert key in dir(df)
76-
assert isinstance(df.__getitem__("A"), pd.Series)
76+
assert isinstance(df.__getitem__("A"), Series)
7777

7878
# DataFrame whose first-level columns are identifiers shall have
7979
# them in __dir__.
@@ -85,7 +85,7 @@ def test_tab_completion(self):
8585
assert key in dir(df)
8686
for key in list("EFGH"):
8787
assert key not in dir(df)
88-
assert isinstance(df.__getitem__("A"), pd.DataFrame)
88+
assert isinstance(df.__getitem__("A"), DataFrame)
8989

9090
def test_not_hashable(self):
9191
empty_frame = DataFrame()

pandas/tests/frame/test_constructors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2208,7 +2208,7 @@ class DatetimeSubclass(datetime):
22082208

22092209
def test_with_mismatched_index_length_raises(self):
22102210
# GH#33437
2211-
dti = pd.date_range("2016-01-01", periods=3, tz="US/Pacific")
2211+
dti = date_range("2016-01-01", periods=3, tz="US/Pacific")
22122212
with pytest.raises(ValueError, match="Shape of passed values"):
22132213
DataFrame(dti, index=range(4))
22142214

pandas/tests/frame/test_query_eval.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ def test_inf(self):
719719
def test_check_tz_aware_index_query(self, tz_aware_fixture):
720720
# https://github.com/pandas-dev/pandas/issues/29463
721721
tz = tz_aware_fixture
722-
df_index = pd.date_range(
722+
df_index = date_range(
723723
start="2019-01-01", freq="1d", periods=10, tz=tz, name="time"
724724
)
725725
expected = DataFrame(index=df_index)

pandas/tests/frame/test_reductions.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ def test_sum_nanops_timedelta(self):
831831
idx = ["a", "b", "c"]
832832
df = DataFrame({"a": [0, 0], "b": [0, np.nan], "c": [np.nan, np.nan]})
833833

834-
df2 = df.apply(pd.to_timedelta)
834+
df2 = df.apply(to_timedelta)
835835

836836
# 0 by default
837837
result = df2.sum()
@@ -861,9 +861,9 @@ def test_sum_bool(self, float_frame):
861861

862862
def test_sum_mixed_datetime(self):
863863
# GH#30886
864-
df = DataFrame(
865-
{"A": pd.date_range("2000", periods=4), "B": [1, 2, 3, 4]}
866-
).reindex([2, 3, 4])
864+
df = DataFrame({"A": date_range("2000", periods=4), "B": [1, 2, 3, 4]}).reindex(
865+
[2, 3, 4]
866+
)
867867
result = df.sum()
868868

869869
expected = Series({"B": 7.0})
@@ -893,7 +893,7 @@ def test_mean_datetimelike(self):
893893
df = DataFrame(
894894
{
895895
"A": np.arange(3),
896-
"B": pd.date_range("2016-01-01", periods=3),
896+
"B": date_range("2016-01-01", periods=3),
897897
"C": pd.timedelta_range("1D", periods=3),
898898
"D": pd.period_range("2016", periods=3, freq="A"),
899899
}
@@ -912,7 +912,7 @@ def test_mean_datetimelike_numeric_only_false(self):
912912
df = DataFrame(
913913
{
914914
"A": np.arange(3),
915-
"B": pd.date_range("2016-01-01", periods=3),
915+
"B": date_range("2016-01-01", periods=3),
916916
"C": pd.timedelta_range("1D", periods=3),
917917
}
918918
)
@@ -983,7 +983,7 @@ def test_idxmax(self, float_frame, int_frame):
983983

984984
def test_idxmax_mixed_dtype(self):
985985
# don't cast to object, which would raise in nanops
986-
dti = pd.date_range("2016-01-01", periods=3)
986+
dti = date_range("2016-01-01", periods=3)
987987

988988
df = DataFrame({1: [0, 2, 1], 2: range(3)[::-1], 3: dti})
989989

@@ -1273,8 +1273,8 @@ def test_min_max_dt64_api_consistency_with_NaT(self):
12731273
# returned NaT for series. These tests check that the API is consistent in
12741274
# min/max calls on empty Series/DataFrames. See GH:33704 for more
12751275
# information
1276-
df = DataFrame({"x": pd.to_datetime([])})
1277-
expected_dt_series = Series(pd.to_datetime([]))
1276+
df = DataFrame({"x": to_datetime([])})
1277+
expected_dt_series = Series(to_datetime([]))
12781278
# check axis 0
12791279
assert (df.min(axis=0).x is pd.NaT) == (expected_dt_series.min() is pd.NaT)
12801280
assert (df.max(axis=0).x is pd.NaT) == (expected_dt_series.max() is pd.NaT)
@@ -1302,7 +1302,7 @@ def test_min_max_dt64_api_consistency_empty_df(self):
13021302
@pytest.mark.parametrize("method", ["min", "max"])
13031303
def test_preserve_timezone(self, initial: str, method):
13041304
# GH 28552
1305-
initial_dt = pd.to_datetime(initial)
1305+
initial_dt = to_datetime(initial)
13061306
expected = Series([initial_dt])
13071307
df = DataFrame([expected])
13081308
result = getattr(df, method)(axis=1)
@@ -1330,7 +1330,7 @@ def test_frame_any_with_timedelta(self):
13301330
df = DataFrame(
13311331
{
13321332
"a": Series([0, 0]),
1333-
"t": Series([pd.to_timedelta(0, "s"), pd.to_timedelta(1, "ms")]),
1333+
"t": Series([to_timedelta(0, "s"), to_timedelta(1, "ms")]),
13341334
}
13351335
)
13361336

0 commit comments

Comments
 (0)