Question about candlestick.py example

Hello, guys,

Part of the code in candlstick.py in the gallery, I just don’t understand. And I could not find any references or posts that explain it.

Below are some of the code:

df = pd.DataFrame(MSFT)[:50]
df["date"] = pd.to_datetime(df["date"])

inc = df.close > df.open
dec = df.open > df.close

p = figure(x_axis_type="datetime", tools=TOOLS, plot_width=1000, title = "MSFT Candlestick")

p.segment(df.date, df.high, df.date, df.low, color="black")
p.vbar(df.date[inc], w, df.open[inc], df.close[inc], fill_color="#D5E1DD", line_color="black")
p.vbar(df.date[dec], w, df.open[dec], df.close[dec], fill_color="#F2583E", line_color="black")

What does df.date[inc] mean in

p.vbar(df.date[inc], w, df.open[inc], df.close[inc], 
       fill_color="#D5E1DD", line_color="black")

Why there is a boolean value “inc” in the bracket after df.date, df.open? And where can I find introductions of this.

Thanks a lot,

Ron

Hello!

So at line 11 and 12 they’re defining the series ‘inc’ and ‘dec’ respectively:
‘inc’ is a boolean series which is True when the ‘close’ column is greater than the ‘open’ column
‘dec’ is a boolean series which is True when the ‘open’ column is greater than the ‘close’ column

Therefore df.date[inc] returns a subset of the ‘date’ series (within df) for which ‘inc’ is True. They’re using this to plot all of the instances of ‘inc’ and they’re using df.date[dec] to plot all of the instances of ‘dec’

Let me know if this is not clear!

3 Likes

Thanks, Alexandre, your explanation is very clear and detailed. The concept is still a bit confusing, but I think I know how to use it now. And I googled “boolean series”, finally I found some page on pandas website explaining this a little.

Really appreciate you help,

Ron

1 Like