Skip to content

Commit

Permalink
Fix yet another Pandas Timestamp -> int64 conversion change
Browse files Browse the repository at this point in the history
Also bumped examples to work with Pandas v2 w/o warnings.
  • Loading branch information
highfestiva committed Jun 14, 2023
1 parent f77ccfe commit 7e285e6
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
10 changes: 8 additions & 2 deletions finplot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2658,11 +2658,18 @@ def _get_color(ax, style, wanted_color):
def _pdtime2epoch(t):
if isinstance(t, pd.Series):
if isinstance(t.iloc[0], pd.Timestamp):
dtype = str(t.dtype)
if dtype.endswith('[s]'):
return t.view('int64') * int(1e9)
elif dtype.endswith('[ms]'):
return t.view('int64') * int(1e6)
elif dtype.endswith('us'):
return t.view('int64') * int(1e3)
return t.view('int64')
h = np.nanmax(t.values)
if h < 1e10: # handle s epochs
return (t*1e9).astype('int64')
if h < 1e13: # handle ns epochs
if h < 1e13: # handle ms epochs
return (t*1e6).astype('int64')
if h < 1e16: # handle us epochs
return (t*1e3).astype('int64')
Expand Down Expand Up @@ -2753,7 +2760,6 @@ def _x2t(datasrc, x, ts2str):
if not datasrc.timebased():
return '%g' % t, False
s = ts2str(t)

if epoch_period >= 23*60*60: # daylight savings, leap seconds, etc
i = s.index(' ')
elif epoch_period >= 59: # consider leap seconds
Expand Down
2 changes: 1 addition & 1 deletion finplot/examples/embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

combo = QComboBox()
combo.setEditable(True)
[combo.addItem(i) for i in 'AMRK META REVG TSLA TWTR WMT CT=F GC=F ^GSPC ^FTSE ^N225 EURUSD=X ETH-USD'.split()]
[combo.addItem(i) for i in 'AMRK META REVG TSLA WMT CT=F GC=F ^GSPC ^FTSE ^N225 EURUSD=X ETH-USD'.split()]
layout.addWidget(combo, 0, 0, 1, 1)
info = QLabel()
layout.addWidget(info, 0, 1, 1, 1)
Expand Down
3 changes: 2 additions & 1 deletion finplot/examples/snp500.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def update_legend_text(x, y):
# format html with the candle and set legend
fmt = '<span style="color:#%s">%%.2f</span>' % ('0b0' if (row.Open<row.Close).all() else 'a00')
rawtxt = '<span style="font-size:13px">%%s %%s</span> &nbsp; O%s C%s H%s L%s' % (fmt, fmt, fmt, fmt)
hover_label.setText(rawtxt % (symbol, interval.upper(), row.Open, row.Close, row.High, row.Low))
values = [v.iloc[0] for v in (row.Open, row.Close, row.High, row.Low)]
hover_label.setText(rawtxt % tuple([symbol, interval.upper()] + values))

def update_crosshair_text(x, y, xtext, ytext):
ytext = '%s (Close%+.2f)' % (ytext, (y - df.iloc[x].Close))
Expand Down
Binary file added nuts.xcf
Binary file not shown.

0 comments on commit 7e285e6

Please # to comment.