return a customer series using function with pandas

Viewed 674

I want to write a class with a function that takes start and end of a range and returns a Pandas series object containing the numbers within that range. But I need to know how to run the function with pre-determined procedure when no arguments are given.

import pandas as pd
class customseries:
    def range_series(self,a,b):
        self.a = 0
        self.b = 10
        return pd.Series(range(a,b))
c = customseries()
c.range_series(5,10)

Here if I put:

  1. range_series() -> Should Return a pandas series from 1 to 10
  2. range_series(5) -> Should Return a pandas series from 5 to 10
  3. range_series(5, 10) -> Should Return a pandas series from 5 to 15

How can this be done?

1 Answers

you can pass default variables in range_series function,

import pandas as pd
class customseries:
    def range_series(self,a=0,b=10):
        return pd.Series(range(a,b))
c = customseries()
print(c.range_series())
print(c.range_series(5))
print(c.range_series(5, 10))
Related