Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
An Object of Objects?
#1
Bare with me, relatively new to playing with python, I'm trying to backwards engineer an object so that I can recreate it slightly differently. I'm trying to understand what creates this structure.

The object in the code I'm working with is called s

So... if I type
print type(s)
I get the following results:
Output:
<class 'pandas.core.series.Series'> <class 'pandas.core.series.Series'> <class 'pandas.core.series.Series'> <class 'pandas.core.series.Series'> <class 'pandas.core.series.Series'> <class 'pandas.core.series.Series'> <class 'pandas.core.series.Series'> <class 'pandas.core.series.Series'> <class 'pandas.core.series.Series'> <class 'pandas.core.series.Series'> <class 'pandas.core.series.Series'> <class 'pandas.core.series.Series'> <class 'pandas.core.series.Series'> <class 'pandas.core.series.Series'>
What is this? it isn't a list... it isn't a tuple...
What kind of object returns multiple objects?
Added points for how do I make one?

I am working with a pandas dataframe iterating through the columns and outputting them to series. I need all of those series as one (nested?) object matching what I'm seeing in s
Reply
#2
You need to post the code that defines s. It appears s is a class with 14 instances.
Reply
#3
s represents self in the apply found here:

def background_gradient(s, m, M, cmap='RdBu'):
    print type(s)
    if M > -1*m:
      max = M
    else: max = -1*m
    rng = max
    norm = colors.Normalize(-2*max,
                            2*max)
    normed = norm(s.values)
    c = [colors.rgb2hex(x) for x in plt.cm.get_cmap(cmap)(normed)]
    return ['background-color: %s' % color for color in c]

...


counts_table.style.applymap(color_negative_red)\
   .apply(background_gradient,
               cmap='RdBu',
               m=df['Dataframe Value'].min().min(),
               M=df['Dataframe Value'].max().max()
               )\
   .highlight_null(null_color='#F4F6F8')
I thought it may be a class of some sort. I've never had to create a class, but when I started playing with it, I couldn't seem to find a way that print class would give the desired outcome; at best I would need to use print class.variable but this leads to having to call each variable independently, where I need to be able to get them all at once. However you say the class has instances, which sounds different than nested objects.
Reply
#4
I forgot my libraries:

import pandas as pd
import numpy as np 
import matplotlib.pyplot as plt
from matplotlib import colors

Okay, here's the full code and the context of what I'm trying to do:
import pandas as pd
import numpy as np 
import matplotlib.pyplot as plt
from matplotlib import colors

def background_gradient(s, m, M, cmap='RdBu'):
    if M > -1*m:
      max = M
    else: max = -1*m
    rng = max
    norm = colors.Normalize(-2*max,
                            2*max)
    normed = norm(s.values)
    c = [colors.rgb2hex(x) for x in plt.cm.get_cmap(cmap)(normed)]
    return ['background-color: %s' % color for color in c]
    
def color_negative_red(val):
    color = 'black'
    if val < 0: color = '#BF0711'
    elif val >= 0: color = '#212B36'
    else: color = '#DFE3E8'
    return 'color: %s' % color
    
df = datasets["Topic Counts and AVG Deviations"]
counts_table = pd.pivot_table(df, values='count', index=['ticket about tag'],columns=['datestamp'], aggfunc=np.sum)
deviation_table =  pd.pivot_table(df, values='AVG Deviation', index=['ticket about tag'],columns=['datestamp'], aggfunc=np.sum)

counts_table.style.applymap(color_negative_red)\
   .apply(background_gradient,
               cmap='RdBu',
               m=df['AVG Deviation'].min().min(),
               M=df['AVG Deviation'].max().max()
               )\
   .highlight_null(null_color='#F4F6F8')
When creating the output heatmap, the color of the table cell is based on the value of the cell. What I wanted to do was to rather than have it based on the 'count' value which is numerically displayed, I wanted to use the 'AVG deviation' value to define the cell coloring.

Being that the apply function seems to do something to the 'self' to turn it into a series, I thought that if I could recreate the series from the data in the deviation_table, I could substitute 's' in the background_gradients with my manually created series.

However, with the way type(s) returns multiple series objects and the fact that I can't figure out how to do this; I'm open to other suggestions on how to replace those values for the background coloring only, while still displaying the numeric 'count' value in the cell.

I hope that makes sense.
Reply
#5
I've realized the error in my ways. Ignore all of the above.

To give more explanation, I was thinking it was somehow importing all of the class objects in s once, not iterating through. :embarassed:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a library for recursive object creation using config objects johsmi96 0 1,823 May-03-2021, 08:09 PM
Last Post: johsmi96
  Player object wont recognize collision with other objects. Jan_97 3 2,659 Dec-22-2019, 04:08 PM
Last Post: joe_momma
  How to redefine object so that all user- objects have the necessary capabilities? AlekseyPython 5 2,977 Mar-03-2019, 04:25 AM
Last Post: AlekseyPython
  error creating new object after loading pickled objects from file arogers 2 3,366 Feb-02-2019, 10:43 AM
Last Post: Larz60+
  AttributeError: 'NoneType' object has no attribute 'n' in list of class objects jdrp 4 5,693 Jun-19-2018, 02:44 PM
Last Post: jdrp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020