I’m trying to compare the dates in pandas.
I want the date entry in the dataframe to lie in the window of 10 days of the date specified in the index.
This is what I’m coding:
placeholder = -1
date_indices = intersections_df.index.astype(np.int64) // 10**9
intersections_con = pd.DataFrame(index=intersections_df.index, columns=intersections_df.columns, dtype=bool)
intersections_df_d = intersections_df.fillna(placeholder).astype(np.int64) // 10**9
intersections_df_d =intersections_df_d.astype(np.int64) // 10**9
print(intersections_df_d)
for stock in intersections_df_d.columns:
for i in range(len(intersections_df_d)):
if intersections_df_d.iloc[i][stock] != placeholder:
intersections_con.iloc[i][stock] = (
intersections_df_d.iloc[i][stock] > date_indices[i]
and intersections_df_d.iloc[i][stock] <= date_indices[i].shift(-10)
)
else:
intersections_con.iloc[i][stock] = False
print(intersections_con)
There are NaN and NaT values in the dataframe so I try to create duplicate dataframe by replace those entries with -1.
This way my original dataframe is intact and the new one is then converted to integer data.
Date is then compared to be in the range, but the issue lien in the fact that the conversion is shown to be not supported.
Atleast this is what I understand from the error.
Can someone please help me convert the date to integer and check the condition of it lying in the required range.
Following is the error that I’m encountering:
Traceback (most recent call last):
File "D:Creating Strategy IndicesTennis Ball Effectprojectsnifty_indexsourceVCP.py", line 143, in <module>
vcp_search()
File "D:Creating Strategy IndicesTennis Ball Effectprojectsnifty_indexsourceVCP.py", line 123, in vcp_search
intersections_df_d = intersections_df.fillna(placeholder).astype(np.int64) // 10**9
File "C:UsersdevbAnacondaenvspyfinancelibsite-packagespandascoregeneric.py", line 6534, in astype
new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors)
File "C:UsersdevbAnacondaenvspyfinancelibsite-packagespandascoreinternalsmanagers.py", line 414, in astype
return self.apply(
File "C:UsersdevbAnacondaenvspyfinancelibsite-packagespandascoreinternalsmanagers.py", line 354, in apply
applied = getattr(b, f)(**kwargs)
File "C:UsersdevbAnacondaenvspyfinancelibsite-packagespandascoreinternalsblocks.py", line 616, in astype
new_values = astype_array_safe(values, dtype, copy=copy, errors=errors)
File "C:UsersdevbAnacondaenvspyfinancelibsite-packagespandascoredtypesastype.py", line 238, in astype_array_safe
new_values = astype_array(values, dtype, copy=copy)
File "C:UsersdevbAnacondaenvspyfinancelibsite-packagespandascoredtypesastype.py", line 183, in astype_array
values = _astype_nansafe(values, dtype, copy=copy)
File "C:UsersdevbAnacondaenvspyfinancelibsite-packagespandascoredtypesastype.py", line 134, in _astype_nansafe
return arr.astype(dtype, copy=True)
ValueError: invalid literal for int() with base 10: '2017-09-07'
Thanks!