diff --git a/pandas/tests/apply/test_series_apply.py b/pandas/tests/apply/test_series_apply.py index 5986f1f6cf51d..f66253badaf75 100644 --- a/pandas/tests/apply/test_series_apply.py +++ b/pandas/tests/apply/test_series_apply.py @@ -2,6 +2,8 @@ Counter, defaultdict, ) +from decimal import Decimal +import math import numpy as np import pytest @@ -37,8 +39,6 @@ def test_apply(datetime_series): tm.assert_series_equal(datetime_series.apply(np.sqrt), np.sqrt(datetime_series)) # element-wise apply - import math - tm.assert_series_equal(datetime_series.apply(math.exp), np.exp(datetime_series)) # empty series @@ -525,8 +525,6 @@ def test_map_type_inference(): def test_map_decimal(string_series): - from decimal import Decimal - result = string_series.map(lambda x: Decimal(str(x))) assert result.dtype == np.object_ assert isinstance(result[0], Decimal) diff --git a/pandas/tests/arrays/test_datetimelike.py b/pandas/tests/arrays/test_datetimelike.py index fbd6f362bd9e7..f80051fb187ba 100644 --- a/pandas/tests/arrays/test_datetimelike.py +++ b/pandas/tests/arrays/test_datetimelike.py @@ -1,5 +1,6 @@ from __future__ import annotations +import array import re import numpy as np @@ -1346,9 +1347,6 @@ def array_likes(request): if name == "memoryview": data = memoryview(arr) elif name == "array": - # stdlib array - import array - data = array.array("i", arr) elif name == "dask": import dask.array diff --git a/pandas/tests/frame/indexing/test_indexing.py b/pandas/tests/frame/indexing/test_indexing.py index c0b04b1f8e80f..1b4f08698071a 100644 --- a/pandas/tests/frame/indexing/test_indexing.py +++ b/pandas/tests/frame/indexing/test_indexing.py @@ -3,6 +3,7 @@ datetime, timedelta, ) +from decimal import Decimal import re import numpy as np @@ -467,8 +468,6 @@ def test_setitem_corner2(self): def test_setitem_ambig(self): # Difficulties with mixed-type data - from decimal import Decimal - # Created as float type dm = DataFrame(index=range(3), columns=range(3)) diff --git a/pandas/tests/frame/methods/test_to_records.py b/pandas/tests/frame/methods/test_to_records.py index 701063c277226..393d304024bb6 100644 --- a/pandas/tests/frame/methods/test_to_records.py +++ b/pandas/tests/frame/methods/test_to_records.py @@ -1,4 +1,6 @@ from collections import abc +import email +from email.parser import Parser import numpy as np import pytest @@ -58,9 +60,6 @@ def test_to_records_with_multindex(self): assert "one" not in r def test_to_records_with_Mapping_type(self): - import email - from email.parser import Parser - abc.Mapping.register(email.message.Message) headers = Parser().parsestr( diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index 25f82eb7ff4b3..0496cfcb6f569 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -1,13 +1,18 @@ +import array from collections import ( OrderedDict, abc, + defaultdict, + namedtuple, ) +from dataclasses import make_dataclass from datetime import ( date, datetime, timedelta, ) import functools +import random import re from typing import Iterator import warnings @@ -466,8 +471,6 @@ def test_constructor_numpy_uints(self, values): assert result[0][0] == value def test_constructor_ordereddict(self): - import random - nitems = 100 nums = list(range(nitems)) random.shuffle(nums) @@ -718,8 +721,6 @@ def test_constructor_subclass_dict(self, dict_subclass): def test_constructor_defaultdict(self, float_frame): # try with defaultdict - from collections import defaultdict - data = {} float_frame.loc[: float_frame.index[10], "B"] = np.nan @@ -1343,8 +1344,6 @@ def __len__(self) -> int: def test_constructor_stdlib_array(self): # GH 4297 # support Array - import array - result = DataFrame({"A": array.array("i", range(10))}) expected = DataFrame({"A": list(range(10))}) tm.assert_frame_equal(result, expected, check_dtype=False) @@ -1544,8 +1543,6 @@ def test_constructor_list_of_tuples(self): def test_constructor_list_of_namedtuples(self): # GH11181 - from collections import namedtuple - named_tuple = namedtuple("Pandas", list("ab")) tuples = [named_tuple(1, 3), named_tuple(2, 4)] expected = DataFrame({"a": [1, 2], "b": [3, 4]}) @@ -1559,8 +1556,6 @@ def test_constructor_list_of_namedtuples(self): def test_constructor_list_of_dataclasses(self): # GH21910 - from dataclasses import make_dataclass - Point = make_dataclass("Point", [("x", int), ("y", int)]) data = [Point(0, 3), Point(1, 3)] @@ -1570,8 +1565,6 @@ def test_constructor_list_of_dataclasses(self): def test_constructor_list_of_dataclasses_with_varying_types(self): # GH21910 - from dataclasses import make_dataclass - # varying types Point = make_dataclass("Point", [("x", int), ("y", int)]) HLine = make_dataclass("HLine", [("x0", int), ("x1", int), ("y", int)]) @@ -1586,8 +1579,6 @@ def test_constructor_list_of_dataclasses_with_varying_types(self): def test_constructor_list_of_dataclasses_error_thrown(self): # GH21910 - from dataclasses import make_dataclass - Point = make_dataclass("Point", [("x", int), ("y", int)]) # expect TypeError diff --git a/pandas/tests/groupby/test_filters.py b/pandas/tests/groupby/test_filters.py index c8aaf71fa419e..9db4a14929724 100644 --- a/pandas/tests/groupby/test_filters.py +++ b/pandas/tests/groupby/test_filters.py @@ -1,3 +1,5 @@ +from string import ascii_lowercase + import numpy as np import pytest @@ -192,8 +194,6 @@ def test_filter_against_workaround(): tm.assert_series_equal(new_way.sort_values(), old_way.sort_values()) # Set up DataFrame of ints, floats, strings. - from string import ascii_lowercase - letters = np.array(list(ascii_lowercase)) N = 1000 random_letters = letters.take(np.random.randint(0, 26, N)) diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py index e32a18afcba70..98727719f8658 100644 --- a/pandas/tests/groupby/test_grouping.py +++ b/pandas/tests/groupby/test_grouping.py @@ -1,4 +1,10 @@ -""" test where we are determining what we are grouping, or getting groups """ +""" +test where we are determining what we are grouping, or getting groups +""" +from datetime import ( + date, + timedelta, +) import numpy as np import pytest @@ -167,14 +173,8 @@ def test_grouper_index_types(self, index): df.groupby(list("abcde"), group_keys=False).apply(lambda x: x) def test_grouper_multilevel_freq(self): - # GH 7885 # with level and freq specified in a Grouper - from datetime import ( - date, - timedelta, - ) - d0 = date.today() - timedelta(days=14) dates = date_range(d0, date.today()) date_index = MultiIndex.from_product([dates, dates], names=["foo", "bar"]) diff --git a/pandas/tests/groupby/test_timegrouper.py b/pandas/tests/groupby/test_timegrouper.py index f16cf4dd27016..25829765a90cd 100644 --- a/pandas/tests/groupby/test_timegrouper.py +++ b/pandas/tests/groupby/test_timegrouper.py @@ -1,6 +1,10 @@ -""" test with the TimeGrouper / grouping with datetimes """ - -from datetime import datetime +""" +test with the TimeGrouper / grouping with datetimes +""" +from datetime import ( + datetime, + timedelta, +) from io import StringIO import numpy as np @@ -763,8 +767,6 @@ def test_first_last_max_min_on_time_data(self): # GH 10295 # Verify that NaT is not in the result of max, min, first and last on # Dataframe with datetime or timedelta values. - from datetime import timedelta as td - df_test = DataFrame( { "dt": [ @@ -774,7 +776,13 @@ def test_first_last_max_min_on_time_data(self): "2015-07-23 12:12", np.nan, ], - "td": [np.nan, td(days=1), td(days=2), td(days=3), np.nan], + "td": [ + np.nan, + timedelta(days=1), + timedelta(days=2), + timedelta(days=3), + np.nan, + ], } ) df_test.dt = pd.to_datetime(df_test.dt) diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py index 834be77cbdcda..cc88390de2ab6 100644 --- a/pandas/tests/indexes/test_common.py +++ b/pandas/tests/indexes/test_common.py @@ -3,6 +3,10 @@ any index subclass except for MultiIndex. Makes use of the `index_flat` fixture defined in pandas/conftest.py. """ +from copy import ( + copy, + deepcopy, +) import re import numpy as np @@ -132,11 +136,6 @@ def test_set_name_methods(self, index_flat): assert index.names == [name] def test_copy_and_deepcopy(self, index_flat): - from copy import ( - copy, - deepcopy, - ) - index = index_flat for func in (copy, deepcopy): diff --git a/pandas/tests/indexing/multiindex/test_slice.py b/pandas/tests/indexing/multiindex/test_slice.py index b059839746eef..767df7dec305d 100644 --- a/pandas/tests/indexing/multiindex/test_slice.py +++ b/pandas/tests/indexing/multiindex/test_slice.py @@ -1,3 +1,8 @@ +from datetime import ( + datetime, + timedelta, +) + import numpy as np import pytest @@ -248,12 +253,7 @@ def test_multiindex_slicers_datetimelike(self): # GH 7429 # buggy/inconsistent behavior when slicing with datetime-like - import datetime - - dates = [ - datetime.datetime(2012, 1, 1, 12, 12, 12) + datetime.timedelta(days=i) - for i in range(6) - ] + dates = [datetime(2012, 1, 1, 12, 12, 12) + timedelta(days=i) for i in range(6)] freq = [1, 2] index = MultiIndex.from_product([dates, freq], names=["date", "frequency"]) diff --git a/pandas/tests/io/excel/test_readers.py b/pandas/tests/io/excel/test_readers.py index ad8f1ac7d7d52..430f8b4fd5d69 100644 --- a/pandas/tests/io/excel/test_readers.py +++ b/pandas/tests/io/excel/test_readers.py @@ -5,6 +5,7 @@ from functools import partial import os from pathlib import Path +import platform from urllib.error import URLError from zipfile import BadZipFile @@ -897,8 +898,6 @@ def test_read_from_file_url(self, read_ext, datapath): url_table = pd.read_excel("file://localhost/" + localtable) except URLError: # fails on some systems - import platform - platform_info = " ".join(platform.uname()).strip() pytest.skip(f"failing on {platform_info}") diff --git a/pandas/tests/io/formats/test_printing.py b/pandas/tests/io/formats/test_printing.py index 5ab7ff085f539..4ded7bebc431e 100644 --- a/pandas/tests/io/formats/test_printing.py +++ b/pandas/tests/io/formats/test_printing.py @@ -1,3 +1,5 @@ +import string + import numpy as np import pytest @@ -19,8 +21,6 @@ def test_adjoin(): def test_repr_binary_type(): - import string - letters = string.ascii_letters try: raw = bytes(letters, encoding=cf.get_option("display.encoding")) diff --git a/pandas/tests/io/formats/test_to_csv.py b/pandas/tests/io/formats/test_to_csv.py index 51f607f425fa2..4e47e4197c710 100644 --- a/pandas/tests/io/formats/test_to_csv.py +++ b/pandas/tests/io/formats/test_to_csv.py @@ -3,6 +3,7 @@ import sys from zipfile import ZipFile +from _csv import Error import numpy as np import pytest @@ -94,8 +95,6 @@ def test_to_csv_doublequote(self): with open(path) as f: assert f.read() == expected - from _csv import Error - with tm.ensure_clean("test.csv") as path: with pytest.raises(Error, match="escapechar"): df.to_csv(path, doublequote=False) # no escapechar set diff --git a/pandas/tests/io/json/test_pandas.py b/pandas/tests/io/json/test_pandas.py index aff09a62b0df3..f68f0a5b6dd88 100644 --- a/pandas/tests/io/json/test_pandas.py +++ b/pandas/tests/io/json/test_pandas.py @@ -5,6 +5,7 @@ import json import os import sys +import time import numpy as np import pytest @@ -1730,8 +1731,6 @@ def test_json_multiindex(self, dataframe, expected): @pytest.mark.single_cpu def test_to_s3(self, s3_resource, s3so): - import time - # GH 28375 mock_bucket_name, target_file = "pandas-test", "test.json" df = DataFrame({"x": [1, 2, 3], "y": [2, 4, 6]}) diff --git a/pandas/tests/io/parser/test_c_parser_only.py b/pandas/tests/io/parser/test_c_parser_only.py index ec08fb0d60648..d5a7610ecb8a9 100644 --- a/pandas/tests/io/parser/test_c_parser_only.py +++ b/pandas/tests/io/parser/test_c_parser_only.py @@ -4,7 +4,7 @@ these tests out of this module as soon as the Python parser can accept further arguments when parsing. """ - +from decimal import Decimal from io import ( BytesIO, StringIO, @@ -169,8 +169,6 @@ def test_unsupported_dtype(c_parser_only, match, kwargs): @td.skip_if_32bit @pytest.mark.slow def test_precise_conversion(c_parser_only): - from decimal import Decimal - parser = c_parser_only normal_errors = [] diff --git a/pandas/tests/io/parser/test_encoding.py b/pandas/tests/io/parser/test_encoding.py index 0dff14dad21c4..775d5571c7a3d 100644 --- a/pandas/tests/io/parser/test_encoding.py +++ b/pandas/tests/io/parser/test_encoding.py @@ -2,7 +2,10 @@ Tests encoding functionality during parsing for all of the parsers defined in parsers.py """ -from io import BytesIO +from io import ( + BytesIO, + TextIOWrapper, +) import os import tempfile import uuid @@ -59,8 +62,6 @@ def test_utf16_bom_skiprows(all_parsers, sep, encoding): utf8 = "utf-8" with tm.ensure_clean(path) as path: - from io import TextIOWrapper - bytes_data = data.encode(encoding) with open(path, "wb") as f: diff --git a/pandas/tests/io/parser/test_python_parser_only.py b/pandas/tests/io/parser/test_python_parser_only.py index 5f067b205a72d..ca5a757328ba7 100644 --- a/pandas/tests/io/parser/test_python_parser_only.py +++ b/pandas/tests/io/parser/test_python_parser_only.py @@ -10,6 +10,7 @@ from io import ( BytesIO, StringIO, + TextIOWrapper, ) from typing import Iterator @@ -108,8 +109,6 @@ def test_sniff_delimiter_encoding(python_parser_only, encoding): """ if encoding is not None: - from io import TextIOWrapper - data = data.encode(encoding) data = BytesIO(data) data = TextIOWrapper(data, encoding=encoding) diff --git a/pandas/tests/io/pytables/test_store.py b/pandas/tests/io/pytables/test_store.py index 2664c7df59223..cf83dfdad3f1f 100644 --- a/pandas/tests/io/pytables/test_store.py +++ b/pandas/tests/io/pytables/test_store.py @@ -1,6 +1,7 @@ -import datetime +import datetime as dt import hashlib import os +import tempfile import time from warnings import ( catch_warnings, @@ -126,8 +127,8 @@ def test_repr(setup_path): df["int2"] = 2 df["timestamp1"] = Timestamp("20010102") df["timestamp2"] = Timestamp("20010103") - df["datetime1"] = datetime.datetime(2001, 1, 2, 0, 0) - df["datetime2"] = datetime.datetime(2001, 1, 3, 0, 0) + df["datetime1"] = dt.datetime(2001, 1, 2, 0, 0) + df["datetime2"] = dt.datetime(2001, 1, 3, 0, 0) df.loc[df.index[3:6], ["obj1"]] = np.nan df = df._consolidate() @@ -441,8 +442,8 @@ def test_table_mixed_dtypes(setup_path): df["int2"] = 2 df["timestamp1"] = Timestamp("20010102") df["timestamp2"] = Timestamp("20010103") - df["datetime1"] = datetime.datetime(2001, 1, 2, 0, 0) - df["datetime2"] = datetime.datetime(2001, 1, 3, 0, 0) + df["datetime1"] = dt.datetime(2001, 1, 2, 0, 0) + df["datetime2"] = dt.datetime(2001, 1, 3, 0, 0) df.loc[df.index[3:6], ["obj1"]] = np.nan df = df._consolidate() @@ -458,14 +459,14 @@ def test_calendar_roundtrip_issue(setup_path): weekmask_egypt = "Sun Mon Tue Wed Thu" holidays = [ "2012-05-01", - datetime.datetime(2013, 5, 1), + dt.datetime(2013, 5, 1), np.datetime64("2014-05-01"), ] bday_egypt = pd.offsets.CustomBusinessDay( holidays=holidays, weekmask=weekmask_egypt ) - dt = datetime.datetime(2013, 4, 30) - dts = date_range(dt, periods=5, freq=bday_egypt) + mydt = dt.datetime(2013, 4, 30) + dts = date_range(mydt, periods=5, freq=bday_egypt) s = Series(dts.weekday, dts).map(Series("Mon Tue Wed Thu Fri Sat Sun".split())) @@ -534,7 +535,6 @@ def test_same_name_scoping(setup_path): # changes what 'datetime' points to in the namespace where # 'select' does the lookup - from datetime import datetime # noqa:F401 # technically an error, but allow it result = store.select("df", "index>datetime.datetime(2013,1,5)") @@ -558,11 +558,11 @@ def test_store_index_name(setup_path): def test_store_index_name_numpy_str(tmp_path, table_format, setup_path): # GH #13492 idx = Index( - pd.to_datetime([datetime.date(2000, 1, 1), datetime.date(2000, 1, 2)]), + pd.to_datetime([dt.date(2000, 1, 1), dt.date(2000, 1, 2)]), name="cols\u05d2", ) idx1 = Index( - pd.to_datetime([datetime.date(2010, 1, 1), datetime.date(2010, 1, 2)]), + pd.to_datetime([dt.date(2010, 1, 1), dt.date(2010, 1, 2)]), name="rows\u05d0", ) df = DataFrame(np.arange(4).reshape(2, 2), columns=idx, index=idx1) @@ -877,8 +877,6 @@ def test_copy(): def do_copy(f, new_f=None, keys=None, propindexes=True, **kwargs): if new_f is None: - import tempfile - fd, new_f = tempfile.mkstemp() try: diff --git a/pandas/tests/io/test_orc.py b/pandas/tests/io/test_orc.py index d5c03dcc85a0d..a519d9536eb32 100644 --- a/pandas/tests/io/test_orc.py +++ b/pandas/tests/io/test_orc.py @@ -1,5 +1,6 @@ """ test orc compat """ import datetime +from decimal import Decimal from io import BytesIO import os @@ -98,8 +99,6 @@ def test_orc_reader_basic(dirpath): def test_orc_reader_decimal(dirpath): - from decimal import Decimal - # Only testing the first 10 rows of data data = { "_col0": np.array( diff --git a/pandas/tests/io/xml/test_xml.py b/pandas/tests/io/xml/test_xml.py index d65b9b8af4365..e3724f2a40409 100644 --- a/pandas/tests/io/xml/test_xml.py +++ b/pandas/tests/io/xml/test_xml.py @@ -8,6 +8,7 @@ import os from tarfile import ReadError from urllib.error import HTTPError +from xml.etree.ElementTree import ParseError from zipfile import BadZipFile import numpy as np @@ -491,8 +492,6 @@ def test_empty_string_lxml(val): @pytest.mark.parametrize("val", ["", b""]) def test_empty_string_etree(val): - from xml.etree.ElementTree import ParseError - with pytest.raises(ParseError, match="no element found"): read_xml(val, parser="etree") @@ -511,8 +510,6 @@ def test_wrong_file_path_lxml(): def test_wrong_file_path_etree(): - from xml.etree.ElementTree import ParseError - filename = os.path.join("data", "html", "books.xml") with pytest.raises( diff --git a/pandas/tests/plotting/frame/test_frame.py b/pandas/tests/plotting/frame/test_frame.py index 06f9ffd2bb23a..a32accb11a987 100644 --- a/pandas/tests/plotting/frame/test_frame.py +++ b/pandas/tests/plotting/frame/test_frame.py @@ -3,10 +3,12 @@ date, datetime, ) +import gc import itertools import re import string import warnings +import weakref import numpy as np import pytest @@ -1782,9 +1784,6 @@ def _check(axes): @td.skip_if_no_scipy def test_memory_leak(self): """Check that every plot type gets properly collected.""" - import gc - import weakref - results = {} for kind in plotting.PlotAccessor._all_kinds: diff --git a/pandas/tests/reshape/concat/test_concat.py b/pandas/tests/reshape/concat/test_concat.py index 3dc6f2404444b..5fa989419a7d4 100644 --- a/pandas/tests/reshape/concat/test_concat.py +++ b/pandas/tests/reshape/concat/test_concat.py @@ -2,6 +2,7 @@ abc, deque, ) +from datetime import datetime from decimal import Decimal from typing import Iterator from warnings import ( @@ -343,11 +344,7 @@ def test_dtype_coerceion(self): tm.assert_series_equal(result.dtypes, df.dtypes) # 12045 - import datetime - - df = DataFrame( - {"date": [datetime.datetime(2012, 1, 1), datetime.datetime(1012, 1, 2)]} - ) + df = DataFrame({"date": [datetime(2012, 1, 1), datetime(1012, 1, 2)]}) result = concat([df.iloc[[0]], df.iloc[[1]]]) tm.assert_series_equal(result.dtypes, df.dtypes) diff --git a/pandas/tests/reshape/concat/test_index.py b/pandas/tests/reshape/concat/test_index.py index 9993700fd0737..e0ea09138ef3c 100644 --- a/pandas/tests/reshape/concat/test_index.py +++ b/pandas/tests/reshape/concat/test_index.py @@ -1,3 +1,5 @@ +from copy import deepcopy + import numpy as np import pytest @@ -240,8 +242,6 @@ def test_concat_multiindex_rangeindex(self): def test_concat_multiindex_dfs_with_deepcopy(self): # GH 9967 - from copy import deepcopy - example_multiindex1 = MultiIndex.from_product([["a"], ["b"]]) example_dataframe1 = DataFrame([0], index=example_multiindex1) diff --git a/pandas/tests/reshape/test_get_dummies.py b/pandas/tests/reshape/test_get_dummies.py index 4345a357a0ba8..be0c4b37c2fd5 100644 --- a/pandas/tests/reshape/test_get_dummies.py +++ b/pandas/tests/reshape/test_get_dummies.py @@ -1,4 +1,5 @@ import re +import unicodedata import numpy as np import pytest @@ -164,8 +165,6 @@ def test_get_dummies_include_na(self, sparse, dtype): def test_get_dummies_unicode(self, sparse): # See GH 6885 - get_dummies chokes on unicode values - import unicodedata - e = "e" eacute = unicodedata.lookup("LATIN SMALL LETTER E WITH ACUTE") s = [e, eacute, eacute] diff --git a/pandas/tests/series/test_arithmetic.py b/pandas/tests/series/test_arithmetic.py index 27410a626811c..c577e15b6d8f5 100644 --- a/pandas/tests/series/test_arithmetic.py +++ b/pandas/tests/series/test_arithmetic.py @@ -1,7 +1,9 @@ from datetime import ( + date, timedelta, timezone, ) +from decimal import Decimal import operator import numpy as np @@ -222,9 +224,6 @@ def test_add_with_duplicate_index(self): tm.assert_series_equal(result, expected) def test_add_na_handling(self): - from datetime import date - from decimal import Decimal - ser = Series( [Decimal("1.3"), Decimal("2.3")], index=[date(2012, 1, 1), date(2012, 1, 2)] )