Skip to content

Commit 34188e1

Browse files
committed
fixed merge conflict
2 parents 59db76f + d3ca427 commit 34188e1

File tree

4 files changed

+155
-4
lines changed

4 files changed

+155
-4
lines changed

pandas/core/matrix.py

+23
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,29 @@ def _float_blockify(dct, index, columns):
749749
# do something with dtype?
750750
return make_block(values, columns)
751751

752+
def select(self, crit, axis=0):
753+
"""
754+
Return data corresponding to axis labels matching criteria
755+
756+
Parameters
757+
----------
758+
crit : function
759+
To be called on each index (label). Should return True or False
760+
axis : {0, 1}
761+
762+
Returns
763+
-------
764+
selection : DataFrame
765+
"""
766+
# HACK until refactor
767+
axis_name = self._get_axis_name(axis)
768+
if axis == 0:
769+
axis = self.index
770+
else:
771+
axis = np.asarray(self.cols())
772+
new_axis = axis[np.asarray([crit(label) for label in axis])]
773+
return self.reindex(**{axis_name : new_axis})
774+
752775
def _reorder_columns(mat, current, desired):
753776
indexer, mask = common.get_indexer(current, desired, None)
754777
return mat.take(indexer[mask], axis=1)

pandas/core/sparse.py

+67-4
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,26 @@
33
float64 data
44
"""
55

6+
# pylint: disable=E1101,E1103
7+
68
from numpy import nan, ndarray
79
import numpy as np
810

911
import operator
1012

11-
from pandas.core.common import (_pickle_array, _unpickle_array, _mut_exclusive,
12-
_ensure_index, _try_sort)
13+
from pandas.core.common import (isnull, _pickle_array, _unpickle_array,
14+
_mut_exclusive, _ensure_index)
1315
from pandas.core.index import Index
1416
from pandas.core.series import Series, TimeSeries
1517
from pandas.core.frame import DataFrame, extract_index
1618
from pandas.core.matrix import DataMatrix
1719
from pandas.core.panel import Panel, WidePanel, LongPanelIndex, LongPanel
1820
import pandas.core.common as common
21+
import pandas.core.datetools as datetools
1922

2023
from pandas.lib.sparse import BlockIndex, IntIndex
2124
import pandas.lib.sparse as splib
2225

23-
from pandas.util.testing import set_trace
24-
2526
def make_sparse(arr, kind='block', fill_value=nan):
2627
"""
2728
Convert ndarray to sparse format
@@ -538,6 +539,44 @@ def valid(self):
538539
dense_valid = self.to_dense().valid()
539540
return dense_valid.to_sparse(fill_value=self.fill_value)
540541

542+
def shift(self, periods, offset=None, timeRule=None):
543+
"""
544+
Analogous to Series.shift
545+
"""
546+
# no special handling of fill values yet
547+
if not isnull(self.fill_value):
548+
dense_shifted = self.to_dense().shift(periods, offset=offset,
549+
timeRule=timeRule)
550+
return dense_shifted.to_sparse(fill_value=self.fill_value,
551+
kind=self.kind)
552+
553+
if periods == 0:
554+
return self.copy()
555+
556+
if timeRule is not None and offset is None:
557+
offset = datetools.getOffset(timeRule)
558+
559+
if offset is not None:
560+
return SparseSeries(self.sp_values,
561+
sparse_index=self.sp_index,
562+
index=self.index.shift(periods, offset),
563+
fill_value=self.fill_value)
564+
565+
int_index = self.sp_index.to_int_index()
566+
new_indices = int_index.indices + periods
567+
start, end = new_indices.searchsorted([0, int_index.length])
568+
569+
new_indices = new_indices[start:end]
570+
571+
new_sp_index = IntIndex(len(self), new_indices)
572+
if isinstance(self.sp_index, BlockIndex):
573+
new_sp_index = new_sp_index.to_block_index()
574+
575+
return SparseSeries(self.sp_values[start:end].copy(),
576+
index=self.index,
577+
sparse_index=new_sp_index,
578+
fill_value=self.fill_value)
579+
541580
class SparseTimeSeries(SparseSeries, TimeSeries):
542581
pass
543582

@@ -780,6 +819,30 @@ def count(self, axis=0):
780819
"""
781820
return self.apply(SparseSeries.count, axis=0)
782821

822+
def shift(self, periods, offset=None, timeRule=None):
823+
"""
824+
Analogous to DataFrame.shift
825+
"""
826+
if timeRule is not None and offset is None:
827+
offset = datetools.getOffset(timeRule)
828+
829+
new_series = {}
830+
if offset is None:
831+
new_index = self.index
832+
for col, s in self.iteritems():
833+
new_series[col] = s.shift(periods)
834+
else:
835+
new_index = self.index.shift(periods, offset)
836+
for col, s in self.iteritems():
837+
new_series[col] = SparseSeries(s.sp_values, index=new_index,
838+
sparse_index=s.sp_index,
839+
fill_value=s.fill_value)
840+
841+
return SparseDataFrame(new_series, index=new_index,
842+
columns=self.columns,
843+
default_fill_value=self.default_fill_value,
844+
default_kind=self.default_kind)
845+
783846
def stack_sparse_frame(frame):
784847
"""
785848
Only makes sense when fill_value is NaN

pandas/core/tests/test_frame.py

+5
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,11 @@ def test_filter(self):
14171417
# pass in None
14181418
self.assertRaises(Exception, self.frame.filter, items=None)
14191419

1420+
# objects
1421+
filtered = self.mixed_frame.filter(like='foo')
1422+
self.assert_('foo' in filtered)
1423+
1424+
14201425
def test_sort(self):
14211426
# what to test?
14221427
sorted = self.frame.sort()

pandas/core/tests/test_sparse.py

+60
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from pandas import Series, DataFrame, DateRange, WidePanel
1818
from pandas.core.datetools import BDay
1919
from pandas.core.series import remove_na
20+
import pandas.core.datetools as datetools
2021
import pandas.util.testing as testing
2122

2223
import pandas.core.sparse as spm
@@ -550,6 +551,29 @@ def test_fill_value_corner(self):
550551
result = cop2 / cop
551552
self.assert_(np.isnan(result.fill_value))
552553

554+
def test_shift(self):
555+
series = SparseSeries([nan, 1., 2., 3., nan, nan],
556+
index=np.arange(6))
557+
558+
shifted = series.shift(0)
559+
self.assert_(shifted is not series)
560+
assert_sp_series_equal(shifted, series)
561+
562+
f = lambda s: s.shift(1)
563+
_dense_series_compare(series, f)
564+
565+
f = lambda s: s.shift(-2)
566+
_dense_series_compare(series, f)
567+
568+
series = SparseSeries([nan, 1., 2., 3., nan, nan],
569+
index=DateRange('1/1/2000', periods=6))
570+
f = lambda s: s.shift(2, timeRule='WEEKDAY')
571+
_dense_series_compare(series, f)
572+
573+
f = lambda s: s.shift(2, offset=datetools.bday)
574+
_dense_series_compare(series, f)
575+
576+
553577
class TestSparseTimeSeries(TestCase):
554578
pass
555579

@@ -855,12 +879,48 @@ def _check(frame):
855879
assert_sp_frame_equal(frame, untransposed)
856880
self._check_all(_check)
857881

882+
def test_shift(self):
883+
def _check(frame):
884+
shifted = frame.shift(0)
885+
self.assert_(shifted is not frame)
886+
assert_sp_frame_equal(shifted, frame)
887+
888+
f = lambda s: s.shift(1)
889+
_dense_frame_compare(frame, f)
890+
891+
f = lambda s: s.shift(-2)
892+
_dense_frame_compare(frame, f)
893+
894+
f = lambda s: s.shift(2, timeRule='WEEKDAY')
895+
_dense_frame_compare(frame, f)
896+
897+
f = lambda s: s.shift(2, offset=datetools.bday)
898+
_dense_frame_compare(frame, f)
899+
900+
self._check_all(_check)
901+
902+
def test_count(self):
903+
result = self.frame.count()
904+
dense_result = self.frame.to_dense().count()
905+
assert_series_equal(result, dense_result)
906+
858907
def _check_all(self, check_func):
859908
check_func(self.frame)
860909
check_func(self.iframe)
861910
check_func(self.zframe)
862911
check_func(self.fill_frame)
863912

913+
def _dense_series_compare(s, f):
914+
result = f(s)
915+
assert(isinstance(result, SparseSeries))
916+
dense_result = f(s.to_dense())
917+
assert_series_equal(result.to_dense(), dense_result)
918+
919+
def _dense_frame_compare(frame, f):
920+
result = f(frame)
921+
assert(isinstance(frame, SparseDataFrame))
922+
dense_result = f(frame.to_dense())
923+
assert_frame_equal(result.to_dense(), dense_result)
864924

865925
def panel_data1():
866926
index = DateRange('1/1/2011', periods=8)

0 commit comments

Comments
 (0)