'I keep getting a Timestamp Type error message
df2 = df2.loc[(df2['timestamp'] >= sd)
& (df2['timestamp'] <= ed)]
I used this code, but I keep getting this error. Can someone fix it?
TypeError: '>=' not supported between instances of 'str' and 'datetime.datetime'
Solution 1:[1]
This means that either column timestamp is not a datetime or sd or ed is not in the right format for implicit comparison. To fix, either convert to datetime first like so:
# convert to datetime
df2.loc[:,'timestamp'] = pd.to_datetime(df2.loc[:,'timestamp'])
or
ed = pd.to_datetime(ed)
sd = pd.to_datetime(sd)
or make sure that sd and ed are in the standard format akin to 'yyyy-mm-dd'
Two side notes. When you do the subset, make sure you add .copy() otherwise you'll keep getting errors later. Second, with datetime, you can also use between to subset date intervals:
df2.loc[df2.timestamp.between(sd,ed),:]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gene Burinsky |
