Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set OHLC x-axis tooltip to datetime format #1493

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Azaya89
Copy link
Collaborator

@Azaya89 Azaya89 commented Feb 6, 2025

import pandas as pd
import hvplot.pandas  # noqa
from bokeh.sampledata import stocks

df = pd.DataFrame(stocks.AAPL)
df['date'] = pd.to_datetime(df.date)

df.iloc[-50:].hvplot.ohlc(grid=True)

image

@Azaya89 Azaya89 requested a review from hoxbro February 6, 2025 14:00
@Azaya89 Azaya89 self-assigned this Feb 6, 2025
hvplot/converter.py Outdated Show resolved Hide resolved
@Azaya89
Copy link
Collaborator Author

Azaya89 commented Feb 6, 2025

Ah, @%#$!

@Azaya89 Azaya89 requested a review from hoxbro February 6, 2025 15:58
@hoxbro hoxbro requested a review from maximlt February 6, 2025 17:40
assert '{%F}' in x_tooltip
formatter_key = '@' + x_label
formatter = hover_tool.formatters
assert formatter.get(formatter_key) == 'datetime'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert formatter.get(formatter_key) == 'datetime'
assert formatter[formatter_key] == 'datetime'

@@ -32,3 +32,39 @@ def test_ohlc_hover_cols_all():
tooltips = segments.opts.get('plot').kwargs['tools'][0].tooltips
assert len(tooltips) == len(df.columns) + 1
assert tooltips[-1] == ('Volume', '@Volume')


def test_ohlc_date_tooltip_format():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another test could be added with a DataFrame whose date column isn't the index.

@@ -2630,15 +2630,23 @@ def ohlc(self, x=None, y=None, data=None):
seg_cur_opts, seg_compat_opts = self._get_compat_opts('Segments')
tools = seg_cur_opts.pop('tools', [])
if 'hover' in tools:
x_data = data[x] if x in data.columns else data.index
if pd.api.types.is_datetime64_any_dtype(x_data):
x_tooltip = f'@{x}{{%F}}'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two comments on %F:

  • I can never remember what these sort of format codes mean :) I looked up Bokeh's documentation and think I found the place where it's documented as part of the DateFormatter. %F is a strftime format code Equivalent to %Y-%m-%d (the ISO 8601 date format). Could you add a little comment like %F: strftime code for %Y-%m-%d?
  • Are OHLC plots limited to daily time series? Asked ChatGPT that says it's the main use case but hourly also happens ("mainly day traders and algorithmic traders focusing on short-term price movements"). What do you think about using instead %F %T, equivalent to %Y-%m-%d %H:%M:%S?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OHLC hover text date format bug
3 participants