Home > Mobile >  Converting a dataframe's datetime64[ns] index to a comparable datetime dtype
Converting a dataframe's datetime64[ns] index to a comparable datetime dtype

Time:11-25

Trying to create a mask for my dataframe but can not compare the upper bound / lower bound datetimes to the index of the dataframe due to it being datetime64[ns]. I have seen the solution be to convert via pd.Timestamp - however I still get a value error.

Additionally I have tried to convert the index and am thrown the error: "Cannot convert input ... series... to timestamp"

INPUT:

x = yf.Ticker('^GSPC').history(period='max',interval='1d').loc[:,['Open']]
stdate = pd.Timestamp(2015,12,31)
edate = dt.datetime.today()

y = x.index > stdate

ACTUAL OUTPUT:

*"Invalid comparison between dtype=datetime64[ns, TIMEZONE] and Timestamp"*

EXPECTED OUTPUT:

[FALSE, FALSE, FALSE, TRUE, TRUE... TRUE]

CodePudding user response:

Datetime64 Indexes can be refined to just the date by .date

df.index.date >= date or df.index.datetime >= datetime 

would work

CodePudding user response:

use numpy.datetime64

NOTE: seem can't compare between time-aware datetime, use tz_localize to remove it's timezone

or you can just convert datetime to timestamp (int)

import numpy as np
import pandas as pd

s = pd.Series([0, 1669345200 * 10**9], dtype="datetime64[ns]").dt.tz_localize("UTC")
print(s.info())

stdate = np.datetime64("2015-12-31T00:00:00")
edate = np.datetime64("now")
print(stdate)
print(edate)
print(s.dt.tz_localize(None) > stdate)
  • Related