Skip to content Skip to sidebar Skip to footer

Python Pandas: Adding Methods To Class Pandas.core.series.series

I want to work with a time series in Python and, therefore, Pandas' Series class is just perfect and has a lot of useful methods. Now I want to add some methods that I need and are

Solution 1:

You could do something like this. You don't need to sub-class at all, rather just monkey-patch. And this would be more efficient that appending twice (as an append copies).

In [5]: s = Series(np.arange(5))

In [15]: def append2(self, val):
   ....:     if not isinstance(val, Series):
   ....:         val = Series(val)
   ....:     return concat([ self, val, val ])
   ....: 

In [16]: Series.append2 = append2

In [17]: s.append2(3)
Out[17]: 
0    0
1    1
2    2
3    3
4    4
0    3
0    3
dtype: int64

Post a Comment for "Python Pandas: Adding Methods To Class Pandas.core.series.series"