Skip to content

Commit a56b005

Browse files
committed
increased test coverage of DataFrame / DataMatrix, miscellaneous fixes
git-svn-id: http://pandas.googlecode.com/svn/trunk@100 d5231056-7de3-11de-ac95-d976489f1ece
1 parent 0c1139c commit a56b005

File tree

6 files changed

+193
-75
lines changed

6 files changed

+193
-75
lines changed

pandas/core/datetools.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,17 @@ def isAnchored(self):
220220
def apply(self, other):
221221
if isinstance(other, datetime):
222222
n = self.n
223+
223224
if n == 0 and other.weekday() > 4:
224225
n = 1
225226

226227
result = other
227228

228229
while n != 0:
229-
result = result + timedelta(n/abs(n))
230+
k = n // abs(n)
231+
result = result + timedelta(k)
230232
if result.weekday() < 5:
231-
n -= n/abs(n)
233+
n -= k
232234

233235
if self.normalize:
234236
result = datetime(result.year, result.month, result.day)

pandas/core/matrix.py

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sys
66

77
from numpy.lib.format import read_array, write_array
8+
from numpy import NaN
89
import numpy as np
910

1011
from pandas.core.frame import DataFrame, _pfixed
@@ -147,7 +148,7 @@ def handleDict(data, index, columns, objects, dtype):
147148
K = len(columns)
148149

149150
values = np.empty((N, K), dtype=dtype)
150-
values[:] = np.NaN
151+
values[:] = NaN
151152
else:
152153
raise Exception('DataMatrix constructor not properly called!')
153154

@@ -755,9 +756,9 @@ def _combineFrame(self, other, func):
755756
if not self and not other:
756757
return DataMatrix(index=newIndex)
757758
elif not self:
758-
return other * np.NaN
759+
return other * NaN
759760
elif not other:
760-
return self * np.NaN
761+
return self * NaN
761762

762763
myValues = myReindex.values
763764
if self.columns.equals(other.columns):
@@ -774,7 +775,7 @@ def _combineFrame(self, other, func):
774775
else:
775776
T, N = len(newIndex), len(newCols)
776777
resultMatrix = np.empty((T, N), dtype=self.values.dtype)
777-
resultMatrix.fill(np.NaN)
778+
resultMatrix.fill(NaN)
778779

779780
myIndexer = [self.columns.indexMap[idx] for idx in commonCols]
780781
hisIndexer = [hisCols.indexMap[idx] for idx in commonCols]
@@ -804,11 +805,15 @@ def _combineSeries(self, other, func):
804805
resultMatrix = func(myReindex.values.T, other).T
805806
else:
806807
if len(other) == 0:
807-
return self * np.NaN
808+
return self * NaN
809+
810+
newCols = self.columns.union(other.index)
808811

809812
# Operate column-wise
810-
other = other.reindex(self.columns).view(np.ndarray)
811-
resultMatrix = func(self.values, other)
813+
this = self.reindex(columns=newCols)
814+
other = other.reindex(newCols).values()
815+
816+
resultMatrix = func(this.values, other)
812817

813818
# TODO: deal with objects
814819
return DataMatrix(resultMatrix, index=newIndex, columns=newCols)
@@ -1190,7 +1195,7 @@ def merge(self, otherFrame, on=None):
11901195
fillVec, mask = tseries.getMergeVec(self[on], indexMap)
11911196

11921197
tmpMatrix = otherM.take(fillVec, axis=0)
1193-
tmpMatrix[-mask] = np.NaN
1198+
tmpMatrix[-mask] = NaN
11941199

11951200
seriesDict = dict((col, tmpMatrix[:, j])
11961201
for j, col in enumerate(otherFrame.columns))
@@ -1201,7 +1206,7 @@ def merge(self, otherFrame, on=None):
12011206
objM = objects.asMatrix()
12021207

12031208
tmpMat = objM.take(fillVec, axis=0)
1204-
tmpMat[-mask] = np.NaN
1209+
tmpMat[-mask] = NaN
12051210
objDict = dict((col, tmpMat[:, j])
12061211
for j, col in enumerate(objects.columns))
12071212

@@ -1229,7 +1234,7 @@ def _reindex_index(self, index, method):
12291234
index.indexMap, method)
12301235

12311236
tmpMatrix = self.values.take(fillVec, axis=0)
1232-
tmpMatrix[-mask] = np.NaN
1237+
tmpMatrix[-mask] = NaN
12331238

12341239
if self.objects is not None and len(self.objects.columns) > 0:
12351240
newObjects = self.objects.reindex(index)
@@ -1252,37 +1257,11 @@ def _reindex_columns(self, columns):
12521257
columns.indexMap, '')
12531258

12541259
newValues = self.values.take(indexer, axis=1)
1255-
newValues[:, -mask] = np.NaN
1260+
newValues[:, -mask] = NaN
12561261

12571262
return DataMatrix(newValues, index=self.index, columns=columns,
12581263
objects=self.objects)
12591264

1260-
1261-
def _withColumns(self, columns):
1262-
"""
1263-
Utility method, force values matrix to have particular columns
1264-
Can make this as cute as we like
1265-
"""
1266-
if len(columns) == 0:
1267-
return DataMatrix(index=self.index)
1268-
1269-
T, N = len(self.index), len(columns)
1270-
1271-
resultMatrix = np.empty((T, N), dtype=self.values.dtype)
1272-
resultMatrix.fill(np.NaN)
1273-
1274-
if not isinstance(columns, Index):
1275-
columns = Index(columns)
1276-
1277-
overlap = self.columns.intersection(columns)
1278-
thisIndexer = [self.columns.indexMap[col] for col in overlap]
1279-
resultIndexer = [columns.indexMap[idx] for idx in overlap]
1280-
1281-
resultMatrix[:, resultIndexer] = self.values[:, thisIndexer]
1282-
1283-
return DataMatrix(resultMatrix, index=self.index, columns=columns,
1284-
objects=self.objects)
1285-
12861265
@property
12871266
def T(self):
12881267
"""
@@ -1324,17 +1303,27 @@ def shift(self, periods, offset=None, timeRule=None):
13241303
if timeRule is not None and offset is None:
13251304
offset = datetools.getOffset(timeRule)
13261305

1306+
N = len(self)
1307+
13271308
if offset is None:
1309+
newIndex = self.index
1310+
indexer = np.zeros(N, dtype=int)
13281311
if periods > 0:
1329-
newIndex = self.index[periods:]
1330-
newValues = self.values[:-periods].copy()
1312+
indexer[periods:] = np.arange(N - periods)
1313+
newValues = self.values.take(indexer, axis=0)
1314+
newValues[:periods] = NaN
13311315
else:
1332-
newIndex = self.index[:periods]
1333-
newValues = self.values[-periods:].copy()
1316+
indexer[:periods] = np.arange(-periods, N)
1317+
newValues = self.values.take(indexer, axis=0)
1318+
newValues[periods:] = NaN
13341319
else:
13351320
offset = periods * offset
1336-
newIndex = Index([idx + offset for idx in self.index])
1321+
newIndex = Index([x + offset for x in self.index])
13371322
newValues = self.values.copy()
1323+
1324+
if self.objects is not None:
1325+
pass
1326+
13381327
return DataMatrix(data=newValues, index=newIndex, columns=self.columns)
13391328

13401329
def apply(self, func, axis=0):

pandas/core/panel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ def toWide(self):
10441044

10451045
I, N, K = self.dims
10461046

1047-
values = np.empty((I, N, K), dtype=float)
1047+
values = np.empty((I, N, K), dtype=self.values.dtype)
10481048

10491049
mask = self.index.mask
10501050
notmask = -mask

pandas/core/tests/common.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def assert_frame_equal(left, right):
7070

7171
for col in right:
7272
assert(col in left)
73-
73+
7474
def assert_contains_all(iterable, dic):
7575
for k in iterable:
7676
assert(k in dic)
@@ -137,8 +137,8 @@ def makeTimeDataFrame():
137137

138138
def makeDataMatrix():
139139
data = getSeriesData()
140-
return DataFrame(data)
140+
return DataMatrix(data)
141141

142142
def makeTimeDataMatrix():
143143
data = getTimeSeriesData()
144-
return DataFrame(data)
144+
return DataMatrix(data)

0 commit comments

Comments
 (0)