Home > other >  Can't get object from pandas dataframe
Can't get object from pandas dataframe

Time:12-27

I have a pandas dataframe, one of the columns has an object type, like this: `

trade_data['history'].to_string()

out:
1     [{'status': {'status': 1, 'dt': 738415.125, 'b...
2     [{'status': {'status': 1, 'dt': 738416.8333333...
3     [{'status': {'status': 1, 'dt': 738418.4583333...
4     [{'status': {'status': 1, 'dt': 738420.0833333...
5     [{'status': {'status': 1, 'dt': 738420.5833333...
6     [{'status': {'status': 1, 'dt': 738421.0416666...
7     [{'status': {'status': 1, 'dt': 738421.7083333...

Here it looks like a dict. But when i try to get the value, i get a list like this

trade_data.iloc[0]['history']

out:

[TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738415.125), ('barlen', 0), ('size', -0.050256307), ('price', 19898.0), ('value', -999.999996686), ('pnl', 0.0), ('pnlcomm', -0.39999999867440006), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f7fc9d028f0>), ('size', -0.050256307), ('price', 19898.0), ('commission', 0.39999999867440006)]))]), TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738415.25), ('barlen', 3), ('size', -0.100634395), ('price', 19873.970956808553), ('value', -2000.005043486), ('pnl', 0.0), ('pnlcomm', -0.8000020173944), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f7fc9d03010>), ('size', -0.050378088), ('price', 19850.0), ('commission', 0.40000201872)]))]), TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738416.0416666666), ('barlen', 22), ('size', -0.15069948), ('price', 19911.919904985738), ('value', -3000.715975483), ('pnl', 0.0), ('pnlcomm', -1.2002863901932002), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f7fc9d34040>), ('size', -0.050065085), ('price', <backtrader.linebuffer.LineBuffer object at 0x7f7fc9d701f0>), ('commission', 0.4002843727988)]))]), TradeHistory([('status', AutoOrderedDict([('status', 2), ('dt', 738416.8333333334), ('barlen', 41), ('size', 0.0), ('price', 19911.919904985738), ('value', 0.0), ('pnl', 29.600377543000118), ('pnlcomm', 27.211644913630916), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.BuyOrder object at 0x7f7fc9d35180>), ('size', 0.15069948), ('price', 19715.5), ('commission', 1.188446239176)]))])]

trade_data['history'].dtypes

out:
object

I get data from Backtrader, If it matters.

Also, I tried

trade_data['history'].values

out:
[list([TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738415.125), ('barlen', 0), ('size', -0.050256307), ('price', 19898.0), ('value', -999.999996686), ('pnl', 0.0), ('pnlcomm', -0.39999999867440006), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f872522e830>), ('size', -0.050256307), ('price', 19898.0), ('commission', 0.39999999867440006)]))]), TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738415.25), ('barlen', 3), ('size', -0.100634395), ('price', 19873.970956808553), ('value', -2000.005043486), ('pnl', 0.0), ('pnlcomm', -0.8000020173944), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f872522ef50>), ('size', -0.050378088), ('price', 19850.0), ('commission', 0.40000201872)]))]), TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738416.0416666666), ('barlen', 22), ('size', -0.15069948), ('price', 19911.919904985738), ('value', -3000.715975483), ('pnl', 0.0), ('pnlcomm', -1.2002863901932002), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f872522ff40>), ('size', -0.050065085), ('price', <backtrader.linebuffer.LineBuffer object at 0x7f87253b3c70>), ('commission', 0.4002843727988)]))]), TradeHistory([('status', AutoOrderedDict([('status', 2), ('dt', 738416.8333333334), ('barlen', 41), ('size', 0.0), ('price', 19911.919904985738), ('value', 0.0), ('pnl', 29.600377543000118), ('pnlcomm', 27.211644913630916), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.BuyOrder object at 0x7f87252610c0>), ('size', 0.15069948), ('price', 19715.5), ('commission', 1.188446239176)]))])])
 list([TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738416.8333333334), ('barlen', 0), ('size', 0.050721256), ('price', 19714.0), ('value', 999.9188407839999), ('pnl', 0.0), ('pnlcomm', -0.3999675363136), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.BuyOrder object at 0x7f8725261150>), ('size', 0.050721256), ('price', <backtrader.linebuffer.LineBuffer object at 0x7f87253b3c70>), ('commission', 0.3999675363136)]))]), TradeHistory([('status', AutoOrderedDict([('status', 2), ('dt', 738416.9166666666), ('barlen', 2), ('size', 0.0), ('price', 19714.0), ('value', 0.0), ('pnl', -17.897857440951917), ('pnlcomm', -18.690633370602736), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f8725261510>), ('size', -0.050721256), ('price', 19361.133), ('commission', 0.39280839333721923)]))])])
 list([TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738418.4583333334), ('barlen', 0), ('size', 0.051808908), ('price', 19209.6), ('value', 995.2283991167999), ('pnl', 0.0), ('pnlcomm', -0.39809135964672), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.BuyOrder object at 0x7f8725262fb0>), ('size', 0.051808908), ('price', <backtrader.linebuffer.LineBuffer object at 0x7f87253b3c70>), ('commission', 0.39809135964672)]))]), TradeHistory([('status', AutoOrderedDict([('status', 2), ('dt', 738418.5416666666), ('barlen', 2), ('size', 0.0), ('price', 19209.6), ('value', 0.0), ('pnl', -13.101540272855885), ('pnlcomm', -13.892482376040183), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f87252632b0>), ('size', -0.051808908), ('price', 18956.718), ('commission', 0.39285074353757765)]))])])
 list([TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738420.0833333334), ('barlen', 0), ('size', 0.054050548), ('price', 18501.2), ('value', 999.9999986576), ('pnl', 0.0), ('pnlcomm', -0.39999999946304), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.BuyOrder object at 0x7f87252e8d90>), ('size', 0.054050548), ('price', 18501.2), ('commission', 0.39999999946304)]))]), TradeHistory([('status', AutoOrderedDict([('status', 2), ('dt', 738420.5833333334), ('barlen', 12), ('size', 0.0), ('price', 18501.2), ('value', 0.0), ('pnl', 21.187814816), ('pnlcomm', 20.37933969114752), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f87252e9b10>), ('size', -0.054050548), ('price', 18893.2), ('commission', 0.40847512538944)]))])])
 list([TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738420.5833333334), ('barlen', 0), ('size', -0.052929096), ('price', 18893.2), ('value', -999.9999965472001), ('pnl', 0.0), ('pnlcomm', -0.3999999986188801), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f87252e9ba0>), ('size', -0.052929096), ('price', 18893.2), ('commission', 0.3999999986188801)]))]), TradeHistory([('status', AutoOrderedDict([('status', 2), ('dt', 738420.9583333334), ('barlen', 9), ('size', 0.0), ('price', 18893.2), ('value', 0.0), ('pnl', -32.281455650399884), ('pnlcomm', -33.0943682298978), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.BuyOrder object at 0x7f87252ea350>), ('size', 0.052929096), ('price', 19503.1), ('commission', 0.41291258087904004)]))])])
 list([TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738421.0416666666), ('barlen', 0), ('size', -0.051813203), ('price', 19347.1), ('value', -1002.4352197613), ('pnl', 0.0), ('pnlcomm', -0.40097408790452), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f87252ea9b0>), ('size', -0.051813203), ('price', <backtrader.linebuffer.LineBuffer object at 0x7f87253b3c70>), ('commission', 0.40097408790452)]))]), TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738421.25), ('barlen', 5), ('size', -0.10349946700000001), ('price', 19347.349693382475), ('value', -2002.4403811276998), ('pnl', 0.0), ('pnlcomm', -0.80097615245108), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f87252eb1c0>), ('size', -0.051686264), ('price', 19347.6), ('commission', 0.40000206454656)]))]), TradeHistory([('status', AutoOrderedDict([('status', 2), ('dt', 738421.7083333334), ('barlen', 16), ('size', 0.0), ('price', 19347.349693382475), ('value', 0.0), ('pnl', 61.02842898179956), ('pnlcomm', 59.450888048490114), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.BuyOrder object at 0x7f87252ebe80>), ('size', 0.10349946700000001), ('price', 18757.7), ('commission', 0.7765647808583601)]))])])
 list([TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738421.7083333334), ('barlen', 0), ('size', 0.05331144), ('price', 18757.7), ('value', 999.9999980880001), ('pnl', 0.0), ('pnlcomm', -0.3999999992352), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.BuyOrder object at 0x7f87252ebf10>), ('size', 0.05331144), ('price', 18757.7), ('commission', 0.3999999992352)]))]), TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738421.8333333334), ('barlen', 3), ('size', 0.106506608), ('price', 18777.97783873091), ('value', 1999.9787247024), ('pnl', 0.0), ('pnlcomm', -0.79999148988096), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.BuyOrder object at 0x7f8725108700>), ('size', 0.053195168), ('price', 18798.3), ('commission', 0.39999149064576)]))]), TradeHistory([('status', AutoOrderedDict([('status', 2), ('dt', 738422.0416666666), ('barlen', 8), ('size', 0.0), ('price', 18777.97783873091), ('value', 0.0), ('pnl', 38.387341844800034), ('pnlcomm', 36.772003928300194), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f87251090c0>), ('size', -0.106506608), ('price', 19138.4), ('commission', 0.8153464266188801)]))])])
 list([TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738422.0416666666), ('barlen', 0), ('size', -0.052251245), ('price', 19138.4), ('value', -1000.0052273080001), ('pnl', 0.0), ('pnlcomm', -0.40000209092320005), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f8725109150>), ('size', -0.052251245), ('price', 19138.4), ('commission', 0.40000209092320005)]))]), TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738423.4166666666), ('barlen', 33), ('size', -0.10479074199999999), ('price', 19100.195196837143), ('value', -2001.5236270214002), ('pnl', 0.0), ('pnlcomm', -0.80060945080856), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f872510aa70>), ('size', -0.052539497), ('price', <backtrader.linebuffer.LineBuffer object at 0x7f87253b3c70>), ('commission', 0.40060735988536)]))]), TradeHistory([('status', AutoOrderedDict([('status', 2), ('dt', 738424.3333333334), ('barlen', 55), ('size', 0.0), ('price', 19100.195196837143), ('value', 0.0), ('pnl', 20.73758451480036), ('pnlcomm', 19.14466064698916), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.BuyOrder object at 0x7f872510bdf0>), ('size', 0.10479074199999999), ('price', 18902.3), ('commission', 0.7923144170026399)]))])])
 list([TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738424.3333333334), ('barlen', 0), ('size', 0.052903615), ('price', 18902.3), ('value', 1000.0000018145), ('pnl', 0.0), ('pnlcomm', -0.40000000072580005), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.BuyOrder object at 0x7f872510be80>), ('size', 0.052903615), ('price', 18902.3), ('commission', 0.40000000072580005)]))]), TradeHistory([('status', AutoOrderedDict([('status', 2), ('dt', 738424.875), ('barlen', 13), ('size', 0.0), ('price', 18902.3), ('value', 0.0), ('pnl', 10.675949506999961), ('pnlcomm', 9.871679125745562), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f872513cdc0>), ('size', -0.052903615), ('price', 19104.1), ('commission', 0.40427038052860004)]))])])
 list([TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738424.875), ('barlen', 0), ('size', -0.052344785), ('price', 19104.1), ('value', -1000.0000071184999), ('pnl', 0.0), ('pnlcomm', -0.40000000284739995), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f872513ce50>), ('size', -0.052344785), ('price', 19104.1), ('commission', 0.40000000284739995)]))]), TradeHistory([('status', AutoOrderedDict([('status', 2), ('dt', 738425.0416666666), ('barlen', 4), ('size', 0.0), ('price', 19104.1), ('value', 0.0), ('pnl', -18.934731422834986), ('pnlcomm', -19.742305321098918), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.BuyOrder object at 0x7f872513d3c0>), ('size', 0.052344785), ('price', 19465.831), ('commission', 0.40757389541653394)]))])])
 list([TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738426.75), ('barlen', 0), ('size', -0.051711923), ('price', 19547.2), ('value', -1010.8233012656), ('pnl', 0.0), ('pnlcomm', -0.40432932050624004), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f872513f010>), ('size', -0.051711923), ('price', <backtrader.linebuffer.LineBuffer object at 0x7f87253b3c70>), ('commission', 0.40432932050624004)]))]), TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738426.9583333334), ('barlen', 5), ('size', -0.103053214), ('price', 19512.425518227894), ('value', -2010.8181625890002), ('pnl', 0.0), ('pnlcomm', -0.8043272650356), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f872513f820>), ('size', -0.051341291), ('price', 19477.4), ('commission', 0.39999794452936)]))]), TradeHistory([('status', AutoOrderedDict([('status', 2), ('dt', 738427.4166666666), ('barlen', 16), ('size', 0.0), ('price', 19512.425518227894), ('value', 0.0), ('pnl', 4.196895545199978), ('pnlcomm', 2.5899197733468577), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.BuyOrder object at 0x7f872515c5b0>), ('size', 0.103053214), ('price', 19471.7), ('commission', 0.8026485068175202)]))])])
 list([TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738427.4166666666), ('barlen', 0), ('size', 0.051356584), ('price', 19427.8), ('value', 997.7454426351999), ('pnl', 0.0), ('pnlcomm', -0.39909817705407996), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.BuyOrder object at 0x7f872515c640>), ('size', 0.051356584), ('price', <backtrader.linebuffer.LineBuffer object at 0x7f87253b3c70>), ('commission', 0.39909817705407996)]))]), TradeHistory([('status', AutoOrderedDict([('status', 2), ('dt', 738427.5416666666), ('barlen', 3), ('size', 0.0), ('price', 19427.8), ('value', 0.0), ('pnl', -18.539418684495935), ('pnlcomm', -19.330199271130297), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f872515ca90>), ('size', -0.051356584), ('price', 19066.806), ('commission', 0.3916824095802816)]))])])
 list([TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738430.6666666666), ('barlen', 0), ('size', 0.052253429), ('price', 19137.1), ('value', 999.9790961158999), ('pnl', 0.0), ('pnlcomm', -0.39999163844636), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.BuyOrder object at 0x7f872515fb20>), ('size', 0.052253429), ('price', 19137.1), ('commission', 0.39999163844636)]))]), TradeHistory([('status', AutoOrderedDict([('status', 2), ('dt', 738431.5833333334), ('barlen', 22), ('size', 0.0), ('price', 19137.1), ('value', 0.0), ('pnl', 1.740039185700152), ('pnlcomm', 0.939359893133152), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f8725180f10>), ('size', -0.052253429), ('price', 19170.4), ('commission', 0.40068765412064)]))])])
 list([TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738431.5833333334), ('barlen', 0), ('size', -0.052164025), ('price', 19406.6), ('value', -1012.3263675649999), ('pnl', 0.0), ('pnlcomm', -0.404930547026), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f8725180fa0>), ('size', -0.052164025), ('price', <backtrader.linebuffer.LineBuffer object at 0x7f87253b3c70>), ('commission', 0.404930547026)]))]), TradeHistory([('status', AutoOrderedDict([('status', 2), ('dt', 738431.7083333334), ('barlen', 3), ('size', 0.0), ('price', 19406.6), ('value', 0.0), ('pnl', -6.245598713250168), ('pnlcomm', -7.057958046787467), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.BuyOrder object at 0x7f87251813f0>), ('size', 0.052164025), ('price', 19526.33), ('commission', 0.4074287865113001)]))])])
 list([TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 738432.0416666666), ('barlen', 0), ('size', -0.051137555), ('price', 19555.0), ('value', -999.994888025), ('pnl', 0.0), ('pnlcomm', -0.39999795521), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f8725181db0>), ('size', -0.051137555), ('price', 19555.0), ('commission', 0.39999795521)]))]), TradeHistory([('status', AutoOrderedDict([('status', 2), ('dt', 738432.2916666666), ('barlen', 6), ('size', 0.0), ('price', 19555.0), ('value', 0.0), ('pnl', -18.71312346403509), ('pnlcomm', -19.520604623840704), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.BuyOrder object at 0x7f87251822f0>), ('size', 0.051137555), ('price', 19920.937), ('commission', 0.40748320459561405)]))])])]

CodePudding user response:

Are you trying to get the TradeHistory object as a Dict? or just get the object? If it's the latter, it is stored in the dataframe as a List(TradeHistory) there is more than one object on those lists.

CodePudding user response:

To get the object, you need to extract the result of command trade_data.loc[[0]]['history'].values twice. Like this

hist = trade_data.loc[[0]]['history'].values
hist = hist[0][0]
  • Related