Skip to content

Commit d3aba86

Browse files
authored
Merge pull request #158 from bixbyr/fix_deprecation_warnings
Fix several deprecation warnings in pandas 2.1 which became actual errors in 2.2
2 parents caa7150 + 9caf00c commit d3aba86

File tree

5 files changed

+51
-21
lines changed

5 files changed

+51
-21
lines changed

AUTHORS.rst

+1
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ Contributions
2929
- `Anton Ian Sipos <https://github.com/aisipos>`_
3030
- `Chuan-Jhe Hwong <https://github.com/CJHwong>`_
3131
- `Thomas Grainger <https://github.com/graingert/>`_
32+
- `Ryan Smith <https://github.com/bixbyr/>`_

django_pandas/io.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def read_frame(qs, fieldnames=(), index_col=None, coerce_float=False,
8484
"""
8585

8686
if fieldnames:
87-
fieldnames = pd.unique(fieldnames)
87+
fieldnames = pd.unique(pd.Series(fieldnames))
8888
if index_col is not None and index_col not in fieldnames:
8989
# Add it to the field names if not already there
9090
fieldnames = tuple(fieldnames) + (index_col,)
@@ -151,7 +151,7 @@ def read_frame(qs, fieldnames=(), index_col=None, coerce_float=False,
151151
df.set_index(index_col, inplace=True)
152152

153153
if datetime_index:
154-
df.index = pd.to_datetime(df.index, errors="ignore")
154+
df.index = pd.to_datetime(df.index)
155155
return df
156156

157157

django_pandas/managers.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,9 @@ def to_timeseries(self, fieldnames=(), verbose=True,
241241

242242
if freq is not None:
243243
if agg_kwargs is None:
244-
agg_kwargs=dict()
244+
agg_kwargs = dict()
245245
if agg_args is None:
246-
agg_args=[]
246+
agg_args = []
247247
df = df.resample(freq, **rs_kwargs).agg(*agg_args, **agg_kwargs)
248248

249249
return df
@@ -253,7 +253,7 @@ def to_dataframe(self, fieldnames=(), verbose=True, index=None,
253253
"""
254254
Returns a DataFrame from the queryset
255255
256-
Paramaters
256+
Parameters
257257
-----------
258258
259259
fieldnames: The model field names(columns) to utilise in creating

django_pandas/tests/test_manager.py

+44-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
from datetime import datetime
2+
13
from django.test import TestCase
24
import pandas as pd
35
import numpy as np
46
import pickle
57
import django
8+
from pandas.core.indexes.datetimes import bdate_range
9+
610
from .models import (
711
DataFrame, WideTimeSeries, WideTimeSeriesDateField,
812
LongTimeSeries, PivotData, Dude, Car, Spot
@@ -68,8 +72,28 @@ def unpivot(self, frame):
6872
'date': np.tile(np.array(frame.index), K)}
6973
return pd.DataFrame(data, columns=['date', 'variable', 'value'])
7074

75+
def _makeTimeDataFrame(self, n_rows: int) -> pd.DataFrame:
76+
# Beginning in 2.2 pandas._testing.makeTimeDataFrame was removed, however all that is required for the tests
77+
# in this module is a dataframe with columns A, B, C, D of random values indexed by a DatetimeIndex.
78+
data = {}
79+
for c in ['A', 'B', 'C', 'D']:
80+
dt = datetime(2000, 1, 1)
81+
dr = bdate_range(dt, periods=n_rows, freq='B', name=c)
82+
pd.DatetimeIndex(dr, name=c)
83+
84+
data[c] = pd.Series(
85+
np.random.default_rng(2).standard_normal(n_rows),
86+
index=pd.DatetimeIndex(dr, name=c),
87+
name=c,
88+
)
89+
return pd.DataFrame(data)
90+
7191
def setUp(self):
72-
self.ts = tm.makeTimeDataFrame(100)
92+
if PANDAS_VERSIONINFO >= '2.2.0':
93+
self.ts = self._makeTimeDataFrame(100)
94+
else:
95+
self.ts = tm.makeTimeDataFrame(100)
96+
7397
self.ts2 = self.unpivot(self.ts).set_index('date')
7498
self.ts.columns = ['col1', 'col2', 'col3', 'col4']
7599
create_list = []
@@ -87,9 +111,9 @@ def setUp(self):
87111
col4=cols['col4']))
88112
WideTimeSeriesDateField.objects.bulk_create(create_list)
89113

90-
create_list = [LongTimeSeries(date_ix=r[0], series_name=r[1][0],
91-
value=r[1][1])
92-
for r in self.ts2.iterrows()]
114+
create_list = [LongTimeSeries(date_ix=timestamp, series_name=s.iloc[0],
115+
value=s.iloc[1])
116+
for timestamp, s in self.ts2.iterrows()]
93117

94118
LongTimeSeries.objects.bulk_create(create_list)
95119

@@ -125,18 +149,24 @@ def test_longstorage(self):
125149

126150
def test_resampling(self):
127151
qs = LongTimeSeries.objects.all()
128-
rs_kwargs = {'kind': 'period'}
129152
agg_args = None
130153
agg_kwargs = None
131154
if PANDAS_VERSIONINFO >= '0.25.0':
132155
agg_kwargs = {'func': 'sum'}
133156
else:
134-
agg_args= ['sum']
157+
agg_args = ['sum']
158+
159+
if PANDAS_VERSIONINFO >= '2.2.0':
160+
freq = 'ME'
161+
else:
162+
freq = 'M'
163+
135164
df = qs.to_timeseries(index='date_ix', pivot_columns='series_name',
136165
values='value', storage='long',
137-
freq='M', rs_kwargs=rs_kwargs,
166+
freq=freq,
138167
agg_args=agg_args,
139168
agg_kwargs=agg_kwargs)
169+
df.index = pd.PeriodIndex(df.index)
140170

141171
self.assertEqual([d.month for d in qs.dates('date_ix', 'month')],
142172
df.index.month.tolist())
@@ -147,9 +177,10 @@ def test_resampling(self):
147177
qs2 = WideTimeSeries.objects.all()
148178

149179
df1 = qs2.to_timeseries(index='date_ix', storage='wide',
150-
freq='M', rs_kwargs=rs_kwargs,
180+
freq=freq,
151181
agg_args=agg_args,
152-
agg_kwargs = agg_kwargs)
182+
agg_kwargs=agg_kwargs)
183+
df1.index = pd.PeriodIndex(df1.index)
153184

154185
self.assertEqual([d.month for d in qs.dates('date_ix', 'month')],
155186
df1.index.month.tolist())
@@ -222,11 +253,10 @@ def setUp(self):
222253
'value_col_d': np.random.randn(11),
223254
'value_col_e': np.random.randn(11),
224255
'value_col_f': np.random.randn(11)})
225-
226-
create_list = [PivotData(row_col_a=r[1][0], row_col_b=r[1][1],
227-
row_col_c=r[1][2], value_col_d=r[1][3],
228-
value_col_e=r[1][4], value_col_f=r[1][5])
229-
for r in self.data.iterrows()]
256+
create_list = [PivotData(row_col_a=r.iloc[0], row_col_b=r.iloc[1],
257+
row_col_c=r.iloc[2], value_col_d=r.iloc[3],
258+
value_col_e=r.iloc[4], value_col_f=r.iloc[5])
259+
for _, r in self.data.iterrows()]
230260

231261
PivotData.objects.bulk_create(create_list)
232262

django_pandas/utils.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ def get_cache_key_from_pk(pk):
4848

4949
def inner(pk_series):
5050
pk_series = pk_series.astype(object).where(pk_series.notnull(), None)
51-
cache_keys = pk_series.apply(
52-
get_cache_key_from_pk, convert_dtype=False)
51+
cache_keys = pk_series.apply(get_cache_key_from_pk)
5352
unique_cache_keys = list(filter(None, cache_keys.unique()))
5453

5554
if not unique_cache_keys:

0 commit comments

Comments
 (0)