Skip to content

Commit 7198fe6

Browse files
authored
Fix iotools tests for pandas 3 (#2730)
* fix iotools.crn tests * fix iotools.midc tests * fix iotools.psm4 tests * fix iotools.sodapro tests * fix iotools.tmy tests * address pandas4 deprecation warnings * fix one lint issue
1 parent e7484a6 commit 7198fe6

File tree

7 files changed

+29
-16
lines changed

7 files changed

+29
-16
lines changed

pvlib/iotools/bsrn.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ def _parse_bsrn(fbuf, logical_records=('0100',)):
322322
LR_0100 = LR_0100.reindex(sorted(LR_0100.columns), axis='columns')
323323
LR_0100.columns = BSRN_LR0100_COLUMNS
324324
# Set datetime index
325-
LR_0100.index = (start_date+pd.to_timedelta(LR_0100['day']-1, unit='d')
325+
LR_0100.index = (start_date+pd.to_timedelta(LR_0100['day']-1, unit='D')
326326
+ pd.to_timedelta(LR_0100['minute'], unit='minutes'))
327327
# Drop empty, minute, and day columns
328328
LR_0100 = LR_0100.drop(columns=['empty', 'day', 'minute'])
@@ -336,7 +336,7 @@ def _parse_bsrn(fbuf, logical_records=('0100',)):
336336
na_values=[-999.0, -99.9],
337337
colspecs=BSRN_LR0300_COL_SPECS,
338338
names=BSRN_LR0300_COLUMNS)
339-
LR_0300.index = (start_date+pd.to_timedelta(LR_0300['day']-1, unit='d')
339+
LR_0300.index = (start_date+pd.to_timedelta(LR_0300['day']-1, unit='D')
340340
+ pd.to_timedelta(LR_0300['minute'], unit='minutes'))
341341
LR_0300 = LR_0300.drop(columns=['day', 'minute']).astype(float)
342342
dfs.append(LR_0300)
@@ -353,13 +353,13 @@ def _parse_bsrn(fbuf, logical_records=('0100',)):
353353
# Sort columns to match original order and assign column names
354354
LR_0500 = LR_0500.reindex(sorted(LR_0500.columns), axis='columns')
355355
LR_0500.columns = BSRN_LR0500_COLUMNS
356-
LR_0500.index = (start_date+pd.to_timedelta(LR_0500['day']-1, unit='d')
356+
LR_0500.index = (start_date+pd.to_timedelta(LR_0500['day']-1, unit='D')
357357
+ pd.to_timedelta(LR_0500['minute'], unit='minutes'))
358358
LR_0500 = LR_0500.drop(columns=['empty', 'day', 'minute'])
359359
dfs.append(LR_0500)
360360

361361
if len(dfs):
362-
data = pd.concat(dfs, axis='columns')
362+
data = pd.concat(dfs, axis='columns', sort=False)
363363
else:
364364
data = _empty_dataframe_from_logical_records(logical_records)
365365
metadata = {}

pvlib/iotools/solrad.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def get_solrad(station, start, end,
195195
end = pd.to_datetime(end)
196196

197197
# Generate list of filenames
198-
dates = pd.date_range(start.floor('d'), end, freq='d')
198+
dates = pd.date_range(start.floor('D'), end, freq='D')
199199
station = station.lower()
200200
filenames = [
201201
f"{station}/{d.year}/{station}{d.strftime('%y')}{d.dayofyear:03}.dat"

tests/iotools/test_crn.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ def columns_unmapped():
3333

3434
@pytest.fixture
3535
def dtypes():
36+
# None indicates string, which is dtype("O") for pandas 2 and StringDtype
37+
# for pandas 3
3638
return [
3739
dtype('int64'), dtype('int64'), dtype('int64'), dtype('int64'),
38-
dtype('int64'), dtype('O'), dtype('float64'), dtype('float64'),
40+
dtype('int64'), None, dtype('float64'), dtype('float64'),
3941
dtype('float64'), dtype('float64'), dtype('float64'),
4042
dtype('int64'), dtype('float64'), dtype('O'), dtype('int64'),
4143
dtype('float64'), dtype('int64'), dtype('float64'),
@@ -70,7 +72,10 @@ def test_read_crn(testfile, columns_mapped, dtypes):
7072
0.0, 393.0, 0, 4.8, 'C', 0, 81.0, 0, nan, nan, 1223, 0, 0.64, 0]])
7173
expected = pd.DataFrame(values, columns=columns_mapped, index=index)
7274
for (col, _dtype) in zip(expected.columns, dtypes):
73-
expected[col] = expected[col].astype(_dtype)
75+
# use inferred types for strings, to cover both pandas 2 and 3
76+
if _dtype is not None:
77+
expected[col] = expected[col].astype(_dtype)
78+
7479
out = crn.read_crn(testfile)
7580
assert_frame_equal(out, expected)
7681

@@ -94,6 +99,8 @@ def test_read_crn_problems(testfile_problems, columns_mapped, dtypes):
9499
1.64, 0]])
95100
expected = pd.DataFrame(values, columns=columns_mapped, index=index)
96101
for (col, _dtype) in zip(expected.columns, dtypes):
97-
expected[col] = expected[col].astype(_dtype)
102+
# use inferred types for strings, to cover both pandas 2 and 3
103+
if _dtype is not None:
104+
expected[col] = expected[col].astype(_dtype)
98105
out = crn.read_crn(testfile_problems)
99106
assert_frame_equal(out, expected)

tests/iotools/test_midc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_midc__format_index_tz_conversion():
4343
data = pd.read_csv(MIDC_TESTFILE)
4444
data = data.rename(columns={'MST': 'PST'})
4545
data = midc._format_index(data)
46-
assert data.index[0].tz == pytz.timezone('Etc/GMT+8')
46+
assert str(data.index[0].tz) == 'Etc/GMT+8'
4747

4848

4949
def test_midc__format_index_raw():

tests/iotools/test_psm4.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def assert_psm4_equal(data, metadata, expected):
4949
for mf in METADATA_FIELDS:
5050
assert mf in metadata
5151
# check timezone
52-
assert (data.index.tzinfo.zone == 'Etc/GMT%+d' % -metadata['Time Zone'])
52+
assert (str(data.index.tzinfo) == 'Etc/GMT%+d' % -metadata['Time Zone'])
5353

5454

5555
@pytest.mark.remote_data

tests/iotools/test_sodapro.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,28 @@
2626

2727

2828
dtypes_mcclear_verbose = [
29-
'object', 'float64', 'float64', 'float64', 'float64', 'float64', 'float64',
29+
# None indicates string, which differs between pandas 2 and 3
30+
None, 'float64', 'float64', 'float64', 'float64', 'float64', 'float64',
3031
'float64', 'float64', 'float64', 'float64', 'float64', 'float64',
3132
'float64', 'float64', 'float64', 'float64', 'float64', 'int64', 'float64',
3233
'float64', 'float64', 'float64']
3334

3435
dtypes_mcclear = [
35-
'object', 'float64', 'float64', 'float64', 'float64', 'float64']
36+
# None indicates string, which differs between pandas 2 and 3
37+
None, 'float64', 'float64', 'float64', 'float64', 'float64']
3638

3739
dtypes_radiation_verbose = [
38-
'object', 'float64', 'float64', 'float64', 'float64', 'float64', 'float64',
40+
# None indicates string, which differs between pandas 2 and 3
41+
None, 'float64', 'float64', 'float64', 'float64', 'float64', 'float64',
3942
'float64', 'float64', 'float64', 'float64', 'float64', 'float64',
4043
'float64', 'float64', 'float64', 'float64', 'float64', 'float64',
4144
'float64', 'float64', 'float64', 'float64', 'int64', 'float64', 'float64',
4245
'float64', 'float64', 'float64', 'int64', 'int64', 'float64', 'float64',
4346
'float64', 'float64']
4447

4548
dtypes_radiation = [
46-
'object', 'float64', 'float64', 'float64', 'float64', 'float64', 'float64',
49+
# None indicates string, which differs between pandas 2 and 3
50+
None, 'float64', 'float64', 'float64', 'float64', 'float64', 'float64',
4751
'float64', 'float64', 'float64', 'float64']
4852

4953

@@ -154,7 +158,9 @@ def generate_expected_dataframe(values, columns, index, dtypes):
154158
expected = pd.DataFrame(values, columns=columns, index=index)
155159
expected.index.freq = None
156160
for (col, _dtype) in zip(expected.columns, dtypes):
157-
expected[col] = expected[col].astype(_dtype)
161+
if _dtype is not None:
162+
# for None (string), use inferred type for pandas 2/3 compat
163+
expected[col] = expected[col].astype(_dtype)
158164
return expected
159165

160166

tests/iotools/test_tmy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def test_gh865_read_tmy3_feb_leapyear_hr24():
9393
assert all(data.index[:-1].year == 1990)
9494
assert data.index[-1].year == 1991
9595
# let's do a quick sanity check, are the indices monotonically increasing?
96-
assert all(np.diff(data.index.view(np.int64)) == 3600000000000)
96+
assert all(np.diff(data.index) == pd.Timedelta(hours=1))
9797
# according to the TMY3 manual, each record corresponds to the previous
9898
# hour so check that the 1st hour is 1AM and the last hour is midnite
9999
assert data.index[0].hour == 1

0 commit comments

Comments
 (0)