Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dataframe and many-to-many relationship #40

Closed
evdoks opened this issue Feb 3, 2015 · 5 comments
Closed

Dataframe and many-to-many relationship #40

evdoks opened this issue Feb 3, 2015 · 5 comments

Comments

@evdoks
Copy link

evdoks commented Feb 3, 2015

Is it possible to use django-pandas with models that stay in many-to-many relationship? It seems that currently this feature is not supported. E.g., for

class Topping(models.Model):
    name = models.CharField(max_length=30)

class Pizza(models.Model):
    name = models.CharField(max_length=50)
    toppings = models.ManyToManyField(Topping)

qs_pizza_with_toppings = Pizza.objects.all().prefetch_related('toppings')
df_pizza_with_toppings = read_frame(qs_pizza_with_toppings)

df_pizza_with_toppings does not contain any topping names.

@paulochf
Copy link

paulochf commented Mar 4, 2015

I think I'm having the same problem.

I have models like

class Category(models.Model):
    name    = models.CharField()
    showing = models.BooleanField(default=False)

    objects = DataFrameManager()

class Food(models.Model):
    name    = models.CharField()

    objects = DataFrameManager()

class FoodCategory(models.Model):
    food     = models.ForeignKey('Food')
    category = models.ForeignKey('Category')
    primary  = models.BooleanField(default=False)

    objects  = DataFrameManager()

Then, I try

foods = FoodCategory.objects.only("food__name").filter(category__showing=True)
df = foods.to_dataframe()

df.head()

which prints

id  food category    is_primary
0   1   Food object    Category object   True
1   2   Food object    Category object   True
2   4   Food object    Category object   True
3   5   Food object    Category object   True
4   6   Food object    Category object   True

and then I cannot/don't know how to access the inner object.

df.loc[:,"food"]

0     Food object
1     Food object
2     Food object
3     Food object
...

How to access Food name attribute?

Thanks!

@bufke
Copy link

bufke commented Mar 6, 2015

Same for reverse foreign key relations.

@chrisdev
Copy link
Owner

@evdoks sorry for the late response. You are correct!! I'll fix in in the next release. In the mean time here is a workaround. Using pandas and values_list QuerySets

import pandas as pd
pd.DataFrame.from_records(list(Pizza.objects.all().values_list('name', 'toppings__name')))

@chrisdev
Copy link
Owner

@paulochf @bufke again sorry for the very tardy response.
I'm working on an IPython notebook/blog post with some examples of usage.

df = FoodCategory.objects.only("food__name").filter(category__showing=False).to_dataframe()
# Show the frame to see the column names 
df.head()

#to get the food column you specify a list with the column names that you want ['food']
df[['food']]
#to get food and primary
df[['food, 'primary']]

Note, you must define the __str__ method in your Models for the verbose resolution to work
That's why in your example above you have Food Object in the rows of your pandas DataFrame.

@python_2_unicode_compatible
class Category(models.Model):
    name = models.CharField(max_length=200)
    showing = models.BooleanField(default=False)

    objects = DataFrameManager()
    def __str__(self):
        return "{0}-{1}".format(
            self.name,
            self.showing
        )

@python_2_unicode_compatible
class Food(models.Model):
    name = models.CharField(max_length=200)

    objects = DataFrameManager()

    def __str__(self):
        return "{}".format(self.name)

Also you can get a DateFrame with exactly the columns you want by specifying them in a list as the first or fieldnames argument in the to_dataframe method. You can use __ for relationship spanning

FoodCategory.objects.filter(category__showing=False).to_dataframe(
    ['category__name', 'food__name', 'primary']
)

@paulochf
Copy link

Nice, @chrisdev! Thank you so much for explaining this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants