I am having issues with applying a custom function to a column of a data frame in Python. When I try to apply the function to the column of interest, I receive the following error: "TypeError: unsupported operand type(s) for &: 'Timestamp' and 'Timestamp'"
This error is misleading to me as both of the data types appear to be similar. Any idea on what is causing the issue?
import pandas as pd
game_df = pd.DataFrame({'date': ["20151030", "20151219", "20191201"]})
game_df['date'] = pd.to_datetime(game_df['date'], format = "%Y%m%d")
def nba_season(dt):
if dt >= pd.to_datetime(2015-10-27) & dt <= pd.to_datetime(2016-6-19):
return "15_16"
else:
return "other"
print(game_df['date'].apply(nba_season))
Coming from R, I feel like dates in Python are a little trickier to work with. Is there a better way to approach dates in Python?
question from:https://stackoverflow.com/questions/65661167/using-pandas-to-datetime-in-custom-function