Skip to content

Commit d370b4a

Browse files
committed
use ast.parse to fixup inconsistencies in namespace
1 parent 3b36529 commit d370b4a

16 files changed

+155
-108
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ 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/
142+
additional_dependencies: [tokenize-rt]
142143
- id: FrameOrSeriesUnion
143144
name: Check for use of Union[Series, DataFrame] instead of FrameOrSeriesUnion alias
144145
entry: Union\[.*(Series,.*DataFrame|DataFrame,.*Series).*\]

pandas/tests/frame/indexing/test_indexing.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def test_setitem_multi_index(self):
212212
it = ["jim", "joe", "jolie"], ["first", "last"], ["left", "center", "right"]
213213

214214
cols = MultiIndex.from_product(it)
215-
index = pd.date_range("20141006", periods=20)
215+
index = date_range("20141006", periods=20)
216216
vals = np.random.randint(1, 1000, (len(index), len(cols)))
217217
df = DataFrame(vals, columns=cols, index=index)
218218

@@ -1349,7 +1349,7 @@ def test_loc_duplicates(self):
13491349
# gh-17105
13501350

13511351
# insert a duplicate element to the index
1352-
trange = pd.date_range(
1352+
trange = date_range(
13531353
start=Timestamp(year=2017, month=1, day=1),
13541354
end=Timestamp(year=2017, month=1, day=5),
13551355
)
@@ -1413,7 +1413,7 @@ def test_setitem_with_unaligned_tz_aware_datetime_column(self):
14131413
# GH 12981
14141414
# Assignment of unaligned offset-aware datetime series.
14151415
# Make sure timezone isn't lost
1416-
column = Series(pd.date_range("2015-01-01", periods=3, tz="utc"), name="dates")
1416+
column = Series(date_range("2015-01-01", periods=3, tz="utc"), name="dates")
14171417
df = DataFrame({"dates": column})
14181418
df["dates"] = column[[1, 0, 2]]
14191419
tm.assert_series_equal(df["dates"], column)
@@ -1737,7 +1737,7 @@ def test_object_casting_indexing_wraps_datetimelike():
17371737
df = DataFrame(
17381738
{
17391739
"A": [1, 2],
1740-
"B": pd.date_range("2000", periods=2),
1740+
"B": date_range("2000", periods=2),
17411741
"C": pd.timedelta_range("1 Day", periods=2),
17421742
}
17431743
)

pandas/tests/frame/methods/test_describe.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ def test_describe_tz_values(self, tz_naive_fixture):
277277
tm.assert_frame_equal(result, expected)
278278

279279
def test_datetime_is_numeric_includes_datetime(self):
280-
df = DataFrame({"a": pd.date_range("2012", periods=3), "b": [1, 2, 3]})
280+
df = DataFrame({"a": date_range("2012", periods=3), "b": [1, 2, 3]})
281281
result = df.describe(datetime_is_numeric=True)
282282
expected = DataFrame(
283283
{

pandas/tests/frame/methods/test_diff.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def test_diff_datetime_axis0_with_nat(self, tz):
7575
@pytest.mark.parametrize("tz", [None, "UTC"])
7676
def test_diff_datetime_with_nat_zero_periods(self, tz):
7777
# diff on NaT values should give NaT, not timedelta64(0)
78-
dti = pd.date_range("2016-01-01", periods=4, tz=tz)
78+
dti = date_range("2016-01-01", periods=4, tz=tz)
7979
ser = Series(dti)
8080
df = ser.to_frame()
8181

@@ -173,7 +173,7 @@ def test_diff_axis(self):
173173

174174
def test_diff_period(self):
175175
# GH#32995 Don't pass an incorrect axis
176-
pi = pd.date_range("2016-01-01", periods=3).to_period("D")
176+
pi = date_range("2016-01-01", periods=3).to_period("D")
177177
df = DataFrame({"A": pi})
178178

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

pandas/tests/frame/methods/test_drop.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
def test_drop_raise_exception_if_labels_not_in_level(msg, labels, level):
2222
# GH 8594
2323
mi = MultiIndex.from_arrays([[1, 2, 3], [4, 5, 6]], names=["a", "b"])
24-
s = pd.Series([10, 20, 30], index=mi)
24+
s = Series([10, 20, 30], index=mi)
2525
df = DataFrame([10, 20, 30], index=mi)
2626

2727
with pytest.raises(KeyError, match=msg):
@@ -34,7 +34,7 @@ def test_drop_raise_exception_if_labels_not_in_level(msg, labels, level):
3434
def test_drop_errors_ignore(labels, level):
3535
# GH 8594
3636
mi = MultiIndex.from_arrays([[1, 2, 3], [4, 5, 6]], names=["a", "b"])
37-
s = pd.Series([10, 20, 30], index=mi)
37+
s = Series([10, 20, 30], index=mi)
3838
df = DataFrame([10, 20, 30], index=mi)
3939

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

pandas/tests/frame/methods/test_join.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def test_join_multiindex_leftright(self):
304304
tm.assert_frame_equal(df1.join(df2, how="left"), exp)
305305
tm.assert_frame_equal(df2.join(df1, how="right"), exp[["value2", "value1"]])
306306

307-
exp_idx = pd.MultiIndex.from_product(
307+
exp_idx = MultiIndex.from_product(
308308
[["a", "b"], ["x", "y", "z"]], names=["first", "second"]
309309
)
310310
exp = DataFrame(

pandas/tests/frame/methods/test_reset_index.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ def test_reset_index_multiindex_columns(self):
423423
def test_reset_index_datetime(self, tz_naive_fixture):
424424
# GH#3950
425425
tz = tz_naive_fixture
426-
idx1 = pd.date_range("1/1/2011", periods=5, freq="D", tz=tz, name="idx1")
426+
idx1 = date_range("1/1/2011", periods=5, freq="D", tz=tz, name="idx1")
427427
idx2 = Index(range(5), name="idx2", dtype="int64")
428428
idx = MultiIndex.from_arrays([idx1, idx2])
429429
df = DataFrame(
@@ -450,7 +450,7 @@ def test_reset_index_datetime(self, tz_naive_fixture):
450450

451451
tm.assert_frame_equal(df.reset_index(), expected)
452452

453-
idx3 = pd.date_range(
453+
idx3 = date_range(
454454
"1/1/2012", periods=5, freq="MS", tz="Europe/Paris", name="idx3"
455455
)
456456
idx = MultiIndex.from_arrays([idx1, idx2, idx3])
@@ -489,7 +489,7 @@ def test_reset_index_datetime(self, tz_naive_fixture):
489489

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

pandas/tests/frame/methods/test_to_csv.py

Lines changed: 14 additions & 19 deletions
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def test_tab_completion(self):
6565
df = DataFrame([list("abcd"), list("efgh")], columns=list("ABCD"))
6666
for key in list("ABCD"):
6767
assert key in dir(df)
68-
assert isinstance(df.__getitem__("A"), pd.Series)
68+
assert isinstance(df.__getitem__("A"), Series)
6969

7070
# DataFrame whose first-level columns are identifiers shall have
7171
# them in __dir__.
@@ -77,7 +77,7 @@ def test_tab_completion(self):
7777
assert key in dir(df)
7878
for key in list("EFGH"):
7979
assert key not in dir(df)
80-
assert isinstance(df.__getitem__("A"), pd.DataFrame)
80+
assert isinstance(df.__getitem__("A"), DataFrame)
8181

8282
def test_not_hashable(self):
8383
empty_frame = DataFrame()

pandas/tests/frame/test_constructors.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
)
3434
import pandas._testing as tm
3535
from pandas.arrays import IntervalArray, PeriodArray, SparseArray
36+
from pandas.core.construction import array as pd_array
3637

3738
MIXED_FLOAT_DTYPES = ["float16", "float32", "float64"]
3839
MIXED_INT_DTYPES = [
@@ -753,7 +754,7 @@ def test_constructor_extension_scalar_data(self, data, dtype):
753754
assert df["a"].dtype == dtype
754755
assert df["b"].dtype == dtype
755756

756-
arr = pd.array([data] * 2, dtype=dtype)
757+
arr = pd_array([data] * 2, dtype=dtype)
757758
expected = DataFrame({"a": arr, "b": arr})
758759

759760
tm.assert_frame_equal(df, expected)
@@ -2179,7 +2180,7 @@ class DatetimeSubclass(datetime):
21792180

21802181
def test_with_mismatched_index_length_raises(self):
21812182
# GH#33437
2182-
dti = pd.date_range("2016-01-01", periods=3, tz="US/Pacific")
2183+
dti = date_range("2016-01-01", periods=3, tz="US/Pacific")
21832184
with pytest.raises(ValueError, match="Shape of passed values"):
21842185
DataFrame(dti, index=range(4))
21852186

pandas/tests/frame/test_query_eval.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ def test_inf(self):
713713
def test_check_tz_aware_index_query(self, tz_aware_fixture):
714714
# https://github.com/pandas-dev/pandas/issues/29463
715715
tz = tz_aware_fixture
716-
df_index = pd.date_range(
716+
df_index = date_range(
717717
start="2019-01-01", freq="1d", periods=10, tz=tz, name="time"
718718
)
719719
expected = DataFrame(index=df_index)

0 commit comments

Comments
 (0)