I have put some code that outputs the following Pandas df:
group | text | date | from | to | dollar | flow | |
---|---|---|---|---|---|---|---|
1 | whale_alert_io | 17,807 ETH | 2023-05-26 18:20:23 | gemini | unknown | 32,501,222 | outflow |
1 | whale_alert_io | 75,000,000 USDC | 2023-05-26 17:43:23 | usdc | unknown | 74,988,300 | outflow |
1 | whalebotalerts | 1,846 BTC | 2023-05-26 17:20:38 | bitfinex | unknown | 49,679,510 | outflow |
1 | whale_alert_io | 1,846 BTC | 2023-05-26 17:20:20 | bitfinex | unknown | 49,489,576 | outflow |
1 | whale_alert_io | 792,592 FXS | 2023-05-26 16:01:33 | unknown | frax | 5,459,692 | inflow |
1 | whalebotalerts | 500 BTC | 2023-05-26 15:41:03 | kucoin | unknown | 13,456,948 | outflow |
1 | whale_alert_io | 1,215 BTC | 2023-05-26 15:34:30 | gemini | unknown | 32,595,669 | outflow |
I'm trying to figure out how can I create a bar chat plot, where the X axis are blocks of 30 mins (based on date column), and Y is the 'dollar' amount. Note that it should aggregate dollar amounts at the 'flow' column and if the net aggregate is an 'outflow' then the bar should be in red color, otherwise in green.
I tried the following so far:
# Set 'date' column as the index
df.set_index('date', inplace=True)
# Group the data into 30-minute intervals
grouped = df.groupby(pd.Grouper(freq='30Min'))
# Calculate net aggregate of 'dollar' amounts based on 'flow' column
aggregated = grouped.agg({'dollar': 'sum', 'flow': 'last'})
# Determine color for each bar based on 'flow' column
colors = ['red' if flow == 'outflow' else 'green' for flow in aggregated['flow']]
# Plot the bar chart
plt.bar(aggregated.index, aggregated['dollar'], color=colors)
plt.xlabel('Time')
plt.ylabel('Dollar Amount')
plt.title('Net Dollar Amount by 30-Minute Intervals')
plt.xticks(rotation=45)
plt.show()
Error: TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'