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 7896541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:55:15+00:00 2026-06-03T07:55:15+00:00

I have an API for analysing my exercise data (which I scrape runkeeper ‘s

  • 0

I have an API for analysing my exercise data (which I scrape runkeeper‘s website).

My main class is a subclass of a pandas.DataFrame, which is basically a container for tabular data. It supports indexing by column name, returning an array of the column values.

I would like to add some convenience properties based on the types of ‘fitness activities’ that are present in the data. So for example I’d like to add a property ‘running’:

@property
def running(self):
    return self[self['type'] == 'running']

Which would return all rows of the DataFrame which have ‘running’ in the ‘type’ column.

I tried to do this dynamically for all types present in the data. Here’s what I naively did:

class Activities(pandas.DataFrame):
    def __init__(self,data):
        pandas.DataFrame.__init__(self,data)
        # The set of unique types in the 'type' column:
        types = set(self['type'])
        for type in types:
            method = property(lambda self: self[self['type'] == type])
            setattr(self.__class__,type,method)

The result was that all of these properties ended up returning tables of data for the same type of activity (‘walking’).

What’s happening is that when the properties are accessed, the lambdas are called and they look in the scope they were defined in for the name ‘type’. They find that it is bound to the string ‘walking’, since that was the last iteration of the for loop. Each iteration of the for loop doesn’t have its own namespace, so all the lambdas see only the last iteration, rather than the value that ‘type’ had when they were actually defined.

Can anyone thing of a way around this? I can think of two, but they don’t seem particularly ideal:

  1. define __getattr__ to check that the attribute is an activity type and return the appropriate rows.

  2. use a recursive function call instead of a for loop, so that each level of recursion has its own namespace.

Both of these are a little too clever for my tastes, and pandas.DataFrame already has a __getattr__ that I’d have to cautiously interact with if I made one too. And recursion would work, but feels very wrong since the set of types doesn’t have any intrinsic tree-like structure to it. It’s flat, and should look flat in the code!

  • 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-03T07:55:17+00:00Added an answer on June 3, 2026 at 7:55 am

    Modify the lambda to pull the values into the new scope.

    method = property(lambda self=self, type=type: self[self['type'] == type])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have taken a look at the PMD api which I want to use
I have api keys which i don't want to show in public but still
I see many Java packages have api, impl and bundle jars (name-api.jar, name-impl.jar, name-bundle.jar).
I have an API that is dependent on certain state information between requests. As
I have an API consisting of ASP.NET webservices callable through GET/POST/SOAP. A new functionality
I have an API call in my application where I am checking the time
I have an API that can be implemented by various vendors. I can send
I have an API in the form of a JAR that I would like
I have an API that returns a promise from a deferred object. The problem
I have an API call that returns a byte array. I currently stream the

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.