Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8381567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:43:34+00:00 2026-06-09T16:43:34+00:00

I am trying to create a subclass of a Pandas data structure to substitute,

  • 0

I am trying to create a subclass of a Pandas data structure to substitute, in my code, a subclass of a dict with a subclass of a Series, I don’t understand why this example code doesn’t work

from pandas import Series    

class Support(Series):
    def supportMethod1(self):
        print 'I am support method 1'       
    def supportMethod2(self):
        print 'I am support method 2'

class Compute(object):
    supp=None        
    def test(self):
        self.supp()  

class Config(object):
    supp=None        
    @classmethod
    def initializeConfig(cls):
        cls.supp=Support()
    @classmethod
    def setConfig1(cls):
        Compute.supp=cls.supp.supportMethod1
    @classmethod
    def setConfig2(cls):
        Compute.supp=cls.supp.supportMethod2            

Config.initializeConfig()

Config.setConfig1()    
c1=Compute()
c1.test()

Config.setConfig2()    
c1.test()

Probably it is not the best method to change the configuration of some objects, anyway I found this usefull in my code and most of all I want to understand why with dict instead of series it works as I expect.

Thanks a lot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:43:35+00:00Added an answer on June 9, 2026 at 4:43 pm

    Current Answer (Pandas >= 0.13)

    An internal refactor in Pandas 0.13 drastically simplified subclassing. Pandas Series can now be subclassed like any other Python object:

    class MySeries(pd.Series):
        def my_method(self):
            return "my_method"
    

    Legacy Answer (Pandas <= 0.12)

    The problem is that Series uses __new__ which is ensuring that a Series object is instantiated.

    You can modify your class like so:

    class Support(pd.Series):
        def __new__(cls, *args, **kwargs):
            arr = Series.__new__(cls, *args, **kwargs)
            return arr.view(Support)
    
        def supportMethod1(self):
            print 'I am support method 1'       
        def supportMethod2(self):
            print 'I am support method 2'
    

    However, it’s probably best to do a has-a instead of a is-a. Or monkey patch the Series object. The reason is that you will often lose your subclass while using pandas due to the nature of it’s data storage. Something as simple as

    s.ix[:5] 
    s.cumsum()
    

    Will return a Series object instead of your subclass. Internally, the data is stored in contiguous arrays and optimized for speed. The data is only boxed with a class when needed and those classes are hardcoded. Plus, it’s not immediately obvious if something like s.ix[:5] should return the same subclass. That would depend on the semantics of your subclass and what metadata is attached to it.

    http://nbviewer.ipython.org/3366583/subclassing%20pandas%20objects.ipynb has some notes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble with my code. I'm trying to create a subclass which inherits
I'm trying to create a EasyMock for this particular piece of code, such that
I'm trying to create a subclass of JButton or AbstractButton that would call specified
I'm currently trying to create a subclass of UIImageView in order to make it
I'm trying to create my own subclass of socket.socket that will be able to
I am trying to create my own functions in the subclass of BaseHTTPRequestHandler as
I'm trying to create some kind of callback for a class template. The code
I am trying to create a subclass of asp:DropDownList so I can use asp:DdlNoEventValidation
I'm trying to create a subclass of java.lang.RuntimeException called HashException and I have to
I'm a experienced C++ programmer trying to create my first Objective-C subclass of UILabel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.