2

I would like to 'link' two pieces of code. One,

x= df[df['Value']==True].sort_values(by='Date').head(1).Date

Out[111]:
8    2020-03-04

extracts the date where the first value appears; the other one

df[df['Buy']==1].groupby('Date').size().plot(ax=ax, label='Buy')

should plot some information through time.

I would like to add a vertical line at the first date where the value is true, i.e. 2020-03-04. In order to do it, I would need to extract this information from the first code (not using copy and paste) to the other piece of code which generates the plot. Can you give me some guide on how to do it? Thanks a lot

Update:

I tried as follows:

x= df[df['Value']==True].sort_values(by='Date').head(1).Date.tolist()

Out[111]:
8    ['2020-03-04']

df[df['Buy']==1].groupby('Date').size().plot(ax=ax, label='Buy')

ax.axvline(x, color="red", linestyle="--")

but I got a TypeError: unhashable type: 'numpy.ndarray'

Some data:

Date           Buy      Value
0   2020-02-23  0   False
1   2020-02-23  0   False
2   2020-02-25  0   False
3   2020-02-27  1   False
4   2020-03-03  1   False
5   2020-03-03  1   False
6   2020-03-03  0   False
7   2020-03-04  1   False
8   2020-03-04  0   True
9   2020-03-04  0   True
10  2020-03-04  1   False
11  2020-03-05  0   True
12  2020-03-05  1   False
13  2020-03-05  1   False
14  2020-03-05  1   False
15  2020-03-06  0   False
16  2020-03-06  1   False
17  2020-03-06  1   False
18  2020-03-07  1   False
19  2020-03-07  1   False
20  2020-03-07  1   False
21  2020-03-08  1   False
22  2020-03-08  1   False
23  2020-03-09  1   False
24  2020-03-09  1   False
25  2020-03-09  1   False
26  2020-03-10  1   False
27  2020-03-10  1   False
28  2020-03-10  1   False
29  2020-03-10  0   True
30  2020-03-11  1   False
31  2020-03-11  1   False
32  2020-03-13  0   True
33  2020-03-13  0   False
34  2020-03-15  0   True
35  2020-03-16  0   False
36  2020-03-19  0   False
37  2020-03-22  0   True
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • Does this answer your question? [How do you plot a vertical line on a time series plot in Pandas?](https://stackoverflow.com/questions/19213789/how-do-you-plot-a-vertical-line-on-a-time-series-plot-in-pandas) – Trenton McKinney Sep 27 '20 at 23:58

1 Answers1

1
  • Make certain the Date column is in a datetime format.
import pandas as pd
import random  # for test data
import matplotlib.pyplot as plt

# setup sample data
random.seed(365)
rows = 40
data = {'Date': [random.choice(pd.bdate_range('2020-02-23', freq='d', periods=rows).strftime('%Y-%m-%d').tolist()) for _ in range(rows)],
        'Buy': [random.choice([0, 1]) for _ in range(rows)],
        'Value': [random.choice([False, True]) for _ in range(rows)]}

df = pd.DataFrame(data)

# set the Date column to a datetime
df.Date = pd.to_datetime(df.Date)

# extract values
x = df[df['Value']==True].sort_values(by='Date').head(1).Date

# groupby and plot
ax = df[df['Buy']==1].groupby('Date').size().plot(figsize=(7, 5), label='Buy')

# plot the vertical line; axvline works as long as x is one value
ax.axvline(x, color="red", linestyle="--", label='my value') 

# show the legend
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')

enter image description here

package versions

import matplotlib as mpl

print(mpl.__version__)
print(pd.__version__)

[out]:
3.3.1
1.1.0
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • 1
    Thank you so much. The problem was in setting the Date column to a datetime –  Sep 28 '20 at 00:24