quantstats: TypeError: Invalid comparison between dtype=datetime64[ns, America/New_York] and datetime

This is my code:

import quantstats as qs

qs.extend_pandas()

stock = qs.utils.download_returns('GLD', period="10y")

qs.reports.html(stock, title='PTL Investments', output='output/gld.html')

qs.reports.html(stock, "SPY", title="GLD vs. SPY", output='output/gld_vs_spy.html')

stock = qs.utils.download_returns('QQQ', period="10y")

qs.reports.html(stock, "SPY", title="QQQ vs. SPY", output='output/qqq_ovs_spy.html')

stock = qs.utils.download_returns('TQQQ', period="10y")

qs.reports.html(stock, "QQQ", title="TQQQ vs. QQQ", output='output/tqqq_vs_qqq.html')

tickers = {
    'FB': 0.2,
    'AAPL': 0.2, 
    'AMZN': 0.2,
    'MSFT': 0.2,
    'GOOG': 0.2 
}

fmaga = portfolio = qs.utils.make_index(tickers)

qs.reports.html(fmaga, "qqq", output="output/fmaga_vs_qqq.html")`

and this is my output:

TypeError: Cannot compare tz-naive and tz-aware datetime-like objects

The above exception was the direct cause of the following exception:

InvalidComparison                         Traceback (most recent call last)
...
     33     typ = type(right).__name__
---> 34     raise TypeError(f"Invalid comparison between dtype={left.dtype} and {typ}")
     35 return res_values

TypeError: Invalid comparison between dtype=datetime64[ns, America/New_York] and datetime

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 20 (2 by maintainers)

Most upvoted comments

I did the above but I am now experiencing an issue with the tearsheets:


stock = qs.utils.download_returns(“SPY”) bench = qs.utils.download_returns(“SPY”)

stock.index = stock.index.tz_convert(None) bench.index = bench.index.tz_convert(None)

qs.reports.html(stock, bench)


when i run the above for the SAME underlying security(in this case the strategy and bench are both SPY) I do get a working plot but I do not get identical plots or identical output data…is anyone else experiencing this? They should be the same?

Not sure if this is what you’re after, but I download benchmark first, convert, and then add DataFrame to report

# adding benchmark
benchmark_SPY = pd.DataFrame(qs.utils.download_returns('SPY'))
benchmark_SPY.index = benchmark_SPY.index.tz_convert(None)
qs.reports.html(df, benchmark_SPY)

Finally got it to work with:

import quantstats as qs
import pandas as pd
stock = pd.DataFrame(qs.utils.download_returns('AAPL'))
stock.index = stock.index.tz_convert(None) # make timezone agnostic.
stock = stock.squeeze() # back to a series
benchmark = pd.DataFrame(qs.utils.download_returns('MSFT'))
benchmark.index = benchmark.index.tz_convert(None) # make timezone agnostic.
qs.reports.html(stock, benchmark)

I have the same problem if my portfolio has a timezone-aware index, I can’t get it to work even without using a benchmark.

yes, this is because internally metrics() function is always creating default naive (no tz) dates, see my analysis above