5
5
import sys
6
6
7
7
from numpy .lib .format import read_array , write_array
8
+ from numpy import NaN
8
9
import numpy as np
9
10
10
11
from pandas .core .frame import DataFrame , _pfixed
@@ -147,7 +148,7 @@ def handleDict(data, index, columns, objects, dtype):
147
148
K = len (columns )
148
149
149
150
values = np .empty ((N , K ), dtype = dtype )
150
- values [:] = np . NaN
151
+ values [:] = NaN
151
152
else :
152
153
raise Exception ('DataMatrix constructor not properly called!' )
153
154
@@ -755,9 +756,9 @@ def _combineFrame(self, other, func):
755
756
if not self and not other :
756
757
return DataMatrix (index = newIndex )
757
758
elif not self :
758
- return other * np . NaN
759
+ return other * NaN
759
760
elif not other :
760
- return self * np . NaN
761
+ return self * NaN
761
762
762
763
myValues = myReindex .values
763
764
if self .columns .equals (other .columns ):
@@ -774,7 +775,7 @@ def _combineFrame(self, other, func):
774
775
else :
775
776
T , N = len (newIndex ), len (newCols )
776
777
resultMatrix = np .empty ((T , N ), dtype = self .values .dtype )
777
- resultMatrix .fill (np . NaN )
778
+ resultMatrix .fill (NaN )
778
779
779
780
myIndexer = [self .columns .indexMap [idx ] for idx in commonCols ]
780
781
hisIndexer = [hisCols .indexMap [idx ] for idx in commonCols ]
@@ -804,11 +805,15 @@ def _combineSeries(self, other, func):
804
805
resultMatrix = func (myReindex .values .T , other ).T
805
806
else :
806
807
if len (other ) == 0 :
807
- return self * np .NaN
808
+ return self * NaN
809
+
810
+ newCols = self .columns .union (other .index )
808
811
809
812
# 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 )
812
817
813
818
# TODO: deal with objects
814
819
return DataMatrix (resultMatrix , index = newIndex , columns = newCols )
@@ -1190,7 +1195,7 @@ def merge(self, otherFrame, on=None):
1190
1195
fillVec , mask = tseries .getMergeVec (self [on ], indexMap )
1191
1196
1192
1197
tmpMatrix = otherM .take (fillVec , axis = 0 )
1193
- tmpMatrix [- mask ] = np . NaN
1198
+ tmpMatrix [- mask ] = NaN
1194
1199
1195
1200
seriesDict = dict ((col , tmpMatrix [:, j ])
1196
1201
for j , col in enumerate (otherFrame .columns ))
@@ -1201,7 +1206,7 @@ def merge(self, otherFrame, on=None):
1201
1206
objM = objects .asMatrix ()
1202
1207
1203
1208
tmpMat = objM .take (fillVec , axis = 0 )
1204
- tmpMat [- mask ] = np . NaN
1209
+ tmpMat [- mask ] = NaN
1205
1210
objDict = dict ((col , tmpMat [:, j ])
1206
1211
for j , col in enumerate (objects .columns ))
1207
1212
@@ -1229,7 +1234,7 @@ def _reindex_index(self, index, method):
1229
1234
index .indexMap , method )
1230
1235
1231
1236
tmpMatrix = self .values .take (fillVec , axis = 0 )
1232
- tmpMatrix [- mask ] = np . NaN
1237
+ tmpMatrix [- mask ] = NaN
1233
1238
1234
1239
if self .objects is not None and len (self .objects .columns ) > 0 :
1235
1240
newObjects = self .objects .reindex (index )
@@ -1252,37 +1257,11 @@ def _reindex_columns(self, columns):
1252
1257
columns .indexMap , '' )
1253
1258
1254
1259
newValues = self .values .take (indexer , axis = 1 )
1255
- newValues [:, - mask ] = np . NaN
1260
+ newValues [:, - mask ] = NaN
1256
1261
1257
1262
return DataMatrix (newValues , index = self .index , columns = columns ,
1258
1263
objects = self .objects )
1259
1264
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
-
1286
1265
@property
1287
1266
def T (self ):
1288
1267
"""
@@ -1324,17 +1303,27 @@ def shift(self, periods, offset=None, timeRule=None):
1324
1303
if timeRule is not None and offset is None :
1325
1304
offset = datetools .getOffset (timeRule )
1326
1305
1306
+ N = len (self )
1307
+
1327
1308
if offset is None :
1309
+ newIndex = self .index
1310
+ indexer = np .zeros (N , dtype = int )
1328
1311
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
1331
1315
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
1334
1319
else :
1335
1320
offset = periods * offset
1336
- newIndex = Index ([idx + offset for idx in self .index ])
1321
+ newIndex = Index ([x + offset for x in self .index ])
1337
1322
newValues = self .values .copy ()
1323
+
1324
+ if self .objects is not None :
1325
+ pass
1326
+
1338
1327
return DataMatrix (data = newValues , index = newIndex , columns = self .columns )
1339
1328
1340
1329
def apply (self , func , axis = 0 ):
0 commit comments