Skip to content

Commit 9caf00c

Browse files
committed
Fix tests which broke in pandas 2.2.0 because makeTimeDataFrame was a non-public method which was removed. This was done by just taking the parts we needed. Also fixed a couple formatting issues which violate python style guides.
1 parent 13550dc commit 9caf00c

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

django_pandas/io.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

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

Lines changed: 37 additions & 6 deletions
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 = []
@@ -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())

0 commit comments

Comments
 (0)