Skip to content

Commit

Permalink
ruff ICN rule compliance
Browse files Browse the repository at this point in the history
  • Loading branch information
bjlittle committed Dec 18, 2023
1 parent fd548fd commit d6e91f7
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 48 deletions.
6 changes: 3 additions & 3 deletions .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ lint.ignore = [
# https://docs.astral.sh/ruff/rules/#flake8-future-annotations-fa
"FA",

# flake8-import-conventions (ICN)
# https://docs.astral.sh/ruff/rules/#flake8-import-conventions-icn
"ICN",
# flake8-implicit-str-concat (ISC)
# https://docs.astral.sh/ruff/rules/#flake8-implicit-str-concat-isc
"ISC",

# flake8-logging-format (G)
# https://docs.astral.sh/ruff/rules/#flake8-logging-format-g
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/benchmarks/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
"""Plot benchmark tests.
"""
import matplotlib
import matplotlib as mpl
import numpy as np

from iris import coords, cube, plot

from . import ARTIFICIAL_DIM_SIZE

matplotlib.use("agg")
mpl.use("agg")


class AuxSort:
Expand Down
22 changes: 11 additions & 11 deletions lib/iris/pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import cftime
import numpy as np
import numpy.ma as ma
import pandas
import pandas as pd

try:
from pandas.core.indexes.datetimes import DatetimeIndex # pandas >=0.20
Expand Down Expand Up @@ -44,7 +44,7 @@ def _get_dimensional_metadata(name, values, calendar=None, dm_class=None):

# Convert out of NumPy's own datetime format.
if np.issubdtype(values.dtype, np.datetime64):
values = pandas.to_datetime(values)
values = pd.to_datetime(values)

# Convert pandas datetime objects to python datetime objects.
if isinstance(values, DatetimeIndex):
Expand Down Expand Up @@ -87,7 +87,7 @@ def _add_iris_coord(cube, name, points, dim, calendar=None):
cube.add_aux_coord(coord, dim)


def _series_index_unique(pandas_series: pandas.Series):
def _series_index_unique(pandas_series: pd.Series):
"""Find an index grouping of a :class:`pandas.Series` that has just one Series value per group.
Iterates through grouping single index levels, then combinations of 2
Expand Down Expand Up @@ -357,7 +357,7 @@ def as_cubes(
cell_measure_cols = cell_measure_cols or []
ancillary_variable_cols = ancillary_variable_cols or []

is_series = isinstance(pandas_structure, pandas.Series)
is_series = isinstance(pandas_structure, pd.Series)

if copy:
pandas_structure = pandas_structure.copy()
Expand Down Expand Up @@ -604,7 +604,7 @@ def as_series(cube, copy=True):
index = None
if cube.dim_coords:
index = _as_pandas_coord(cube.dim_coords[0])
series = pandas.Series(data, index)
series = pd.Series(data, index)
if not copy:
_assert_shared(data, series)
return series
Expand Down Expand Up @@ -811,16 +811,16 @@ def merge_metadata(meta_var_list):
# dimension over the whole DataFrame
data_frame[meta_var_name] = meta_var.squeeze()
else:
meta_df = pandas.DataFrame(
meta_df = pd.DataFrame(
meta_var.ravel(),
columns=[meta_var_name],
index=pandas.MultiIndex.from_product(
index=pd.MultiIndex.from_product(
[coords[i] for i in meta_var_index],
names=[coord_names[i] for i in meta_var_index],
),
)
# Merge to main data frame
data_frame = pandas.merge(
data_frame = pd.merge(
data_frame,
meta_df,
left_index=True,
Expand All @@ -847,8 +847,8 @@ def merge_metadata(meta_var_list):
# Extract dim coord information: separate lists for dim names and dim values
coord_names, coords = _make_dim_coord_list(cube)
# Make base DataFrame
index = pandas.MultiIndex.from_product(coords, names=coord_names)
data_frame = pandas.DataFrame(data.ravel(), columns=[cube.name()], index=index)
index = pd.MultiIndex.from_product(coords, names=coord_names)
data_frame = pd.DataFrame(data.ravel(), columns=[cube.name()], index=index)

if add_aux_coords:
data_frame = merge_metadata(_make_aux_coord_list(cube))
Expand Down Expand Up @@ -889,7 +889,7 @@ def merge_metadata(meta_var_list):
if cube.coords(dimensions=[1]):
columns = _as_pandas_coord(cube.coord(dimensions=[1]))

data_frame = pandas.DataFrame(data, index, columns)
data_frame = pd.DataFrame(data, index, columns)
if not copy:
_assert_shared(data, data_frame)

Expand Down
8 changes: 4 additions & 4 deletions lib/iris/tests/graphics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@
# Test for availability of matplotlib.
# (And remove matplotlib as an iris.tests dependency.)
try:
import matplotlib
import matplotlib as mpl

# Override any user settings e.g. from matplotlibrc file.
matplotlib.rcdefaults()
mpl.rcdefaults()
# Set backend *after* rcdefaults, as we don't want that overridden (#3846).
matplotlib.use("agg")
mpl.use("agg")
# Standardise the figure size across matplotlib versions.
# This permits matplotlib png image comparison.
matplotlib.rcParams["figure.figsize"] = [8.0, 6.0]
mpl.rcParams["figure.figsize"] = [8.0, 6.0]
import matplotlib.pyplot as plt
except ImportError:
MPL_AVAILABLE = False
Expand Down
56 changes: 28 additions & 28 deletions lib/iris/tests/unit/pandas/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@
# used by matplotlib for handling dates.
default_units_registry = copy.copy(matplotlib.units.registry)
try:
import pandas
import pandas as pd
except ImportError:
# Disable all these tests if pandas is not installed.
pandas = None
pd = None
matplotlib.units.registry = default_units_registry

skip_pandas = pytest.mark.skipif(
pandas is None,
pd is None,
reason='Test(s) require "pandas", ' "which is not available.",
)

if pandas is not None:
if pd is not None:
from iris.coords import AncillaryVariable, AuxCoord, CellMeasure, DimCoord
from iris.cube import Cube, CubeList
import iris.pandas
Expand Down Expand Up @@ -234,7 +234,7 @@ def test_time_standard(self):
days_to_2000 = 365 * 30 + 7
# pandas Timestamp class cannot handle floats in pandas <v0.12
timestamps = [
pandas.Timestamp(int(nanoseconds_per_day * (days_to_2000 + day_offset)))
pd.Timestamp(int(nanoseconds_per_day * (days_to_2000 + day_offset)))
for day_offset in day_offsets
]
assert all(data_frame.columns == timestamps)
Expand Down Expand Up @@ -588,7 +588,7 @@ def test_instance_error(self):
)
class TestSeriesAsCube(tests.IrisTest):
def test_series_simple(self):
series = pandas.Series([0, 1, 2, 3, 4], index=[5, 6, 7, 8, 9])
series = pd.Series([0, 1, 2, 3, 4], index=[5, 6, 7, 8, 9])
self.assertCML(
iris.pandas.as_cube(series),
tests.get_result_path(("pandas", "as_cube", "series_simple.cml")),
Expand All @@ -599,7 +599,7 @@ class Thing:
def __repr__(self):
return "A Thing"

series = pandas.Series(
series = pd.Series(
[0, 1, 2, 3, 4],
index=[Thing(), Thing(), Thing(), Thing(), Thing()],
)
Expand All @@ -609,14 +609,14 @@ def __repr__(self):
)

def test_series_masked(self):
series = pandas.Series([0, float("nan"), 2, np.nan, 4], index=[5, 6, 7, 8, 9])
series = pd.Series([0, float("nan"), 2, np.nan, 4], index=[5, 6, 7, 8, 9])
self.assertCML(
iris.pandas.as_cube(series),
tests.get_result_path(("pandas", "as_cube", "series_masked.cml")),
)

def test_series_datetime_standard(self):
series = pandas.Series(
series = pd.Series(
[0, 1, 2, 3, 4],
index=[
datetime.datetime(2001, 1, 1, 1, 1, 1),
Expand All @@ -634,7 +634,7 @@ def test_series_datetime_standard(self):
)

def test_series_cftime_360(self):
series = pandas.Series(
series = pd.Series(
[0, 1, 2, 3, 4],
index=[
cftime.datetime(2001, 1, 1, 1, 1, 1),
Expand All @@ -650,13 +650,13 @@ def test_series_cftime_360(self):
)

def test_copy_true(self):
series = pandas.Series([0, 1, 2, 3, 4], index=[5, 6, 7, 8, 9])
series = pd.Series([0, 1, 2, 3, 4], index=[5, 6, 7, 8, 9])
cube = iris.pandas.as_cube(series)
cube.data[0] = 99
assert series[5] == 0

def test_copy_false(self):
series = pandas.Series([0, 1, 2, 3, 4], index=[5, 6, 7, 8, 9])
series = pd.Series([0, 1, 2, 3, 4], index=[5, 6, 7, 8, 9])
cube = iris.pandas.as_cube(series, copy=False)
cube.data[0] = 99
assert series[5] == 99
Expand All @@ -668,7 +668,7 @@ def test_copy_false(self):
)
class TestDataFrameAsCube(tests.IrisTest):
def test_data_frame_simple(self):
data_frame = pandas.DataFrame(
data_frame = pd.DataFrame(
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]],
index=[10, 11],
columns=[12, 13, 14, 15, 16],
Expand All @@ -679,7 +679,7 @@ def test_data_frame_simple(self):
)

def test_data_frame_nonotonic(self):
data_frame = pandas.DataFrame(
data_frame = pd.DataFrame(
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]],
index=[10, 10],
columns=[12, 12, 14, 15, 16],
Expand All @@ -690,7 +690,7 @@ def test_data_frame_nonotonic(self):
)

def test_data_frame_masked(self):
data_frame = pandas.DataFrame(
data_frame = pd.DataFrame(
[[0, float("nan"), 2, 3, 4], [5, 6, 7, np.nan, 9]],
index=[10, 11],
columns=[12, 13, 14, 15, 16],
Expand All @@ -701,7 +701,7 @@ def test_data_frame_masked(self):
)

def test_data_frame_multidim(self):
data_frame = pandas.DataFrame(
data_frame = pd.DataFrame(
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]],
index=[0, 1],
columns=["col_1", "col_2", "col_3", "col_4", "col_5"],
Expand All @@ -712,7 +712,7 @@ def test_data_frame_multidim(self):
)

def test_data_frame_cftime_360(self):
data_frame = pandas.DataFrame(
data_frame = pd.DataFrame(
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]],
index=[
cftime.datetime(2001, 1, 1, 1, 1, 1),
Expand All @@ -728,7 +728,7 @@ def test_data_frame_cftime_360(self):
)

def test_data_frame_datetime_standard(self):
data_frame = pandas.DataFrame(
data_frame = pd.DataFrame(
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]],
index=[
datetime.datetime(2001, 1, 1, 1, 1, 1),
Expand All @@ -744,13 +744,13 @@ def test_data_frame_datetime_standard(self):
)

def test_copy_true(self):
data_frame = pandas.DataFrame([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]])
data_frame = pd.DataFrame([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]])
cube = iris.pandas.as_cube(data_frame)
cube.data[0, 0] = 99
assert data_frame[0][0] == 0

def test_copy_false(self):
data_frame = pandas.DataFrame([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]])
data_frame = pd.DataFrame([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]])
cube = iris.pandas.as_cube(data_frame, copy=False)
cube.data[0, 0] = 99
assert data_frame[0][0] == 99
Expand All @@ -759,7 +759,7 @@ def test_copy_false(self):
@skip_pandas
class TestFutureAndDeprecation:
def test_as_cube_deprecation_warning(self):
data_frame = pandas.DataFrame([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]])
data_frame = pd.DataFrame([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]])
with pytest.warns(IrisDeprecation, match="as_cube has been deprecated"):
_ = iris.pandas.as_cube(data_frame)

Expand Down Expand Up @@ -794,10 +794,10 @@ def _create_pandas(index_levels=0, is_series=False):
]

if index_levels == 1:
index = pandas.Index(index_values[0], name=index_names[0])
index = pd.Index(index_values[0], name=index_names[0])
data_length = index_length
elif index_levels > 1:
index = pandas.MultiIndex.from_product(index_values, names=index_names)
index = pd.MultiIndex.from_product(index_values, names=index_names)
data_length = index.nunique()
else:
index = None
Expand All @@ -806,9 +806,9 @@ def _create_pandas(index_levels=0, is_series=False):
data = np.arange(data_length) * 10

if is_series:
class_ = pandas.Series
class_ = pd.Series
else:
class_ = pandas.DataFrame
class_ = pd.DataFrame

return class_(data, index=index)

Expand Down Expand Up @@ -1013,13 +1013,13 @@ def test_multi_phenom(self):
assert result == [expected_cube_0, expected_cube_1]

def test_empty_series(self):
series = pandas.Series(dtype=object)
series = pd.Series(dtype=object)
result = iris.pandas.as_cubes(series)

assert result == CubeList()

def test_empty_dataframe(self):
df = pandas.DataFrame()
df = pd.DataFrame()
result = iris.pandas.as_cubes(df)

assert result == CubeList()
Expand Down Expand Up @@ -1165,7 +1165,7 @@ def _test_dates_common(self, mode=None, alt_calendar=False):
datetime_args = [(1971, 1, 1, i, 0, 0) for i in df.index.values]
if mode == "index":
values = [datetime.datetime(*a) for a in datetime_args]
df.index = pandas.Index(values, name=coord_name)
df.index = pd.Index(values, name=coord_name)
elif mode == "numpy":
values = [datetime.datetime(*a) for a in datetime_args]
df[coord_name] = values
Expand Down

0 comments on commit d6e91f7

Please sign in to comment.