How can I include datetimes into pd.DataFrame?
import pandas as pd
from datetime import datetime
df = pd.DataFrame({"a": ['2002-02-02', '2002-02-03', '2002-02-04']})
df["b"] = df["a"].apply(lambda t: datetime.strptime(t, '%Y-%m-%d')) # datetime.strptime returns datetime.datetime
print(datetime(2002, 2, 2) in df["b"])
outputs False.
Similarly,
f["c"] = df["b"].apply(lambda t: t.to_pydatetime())
print(datetime(2002, 2, 2) in df["c"])
outputs False.
Note that neither this nor this works. Following any of those approaches, I end up with Timestamps instead of datetimes in the data frame.
I am using Python 3.8.5 and Pandas 1.2.1.
datetime(2002, 2, 2) in list(df['b'])?datetime(2002, 2, 2) in list(pd.to_datetime(df['b']).unique())is againFalse.datetime(2002, 2, 2) in list(pd.to_datetime(pd.to_datetime(df['b']).unique()))pandasyou'll want to work with the built-in datatype (datetime64 fromnumpy). Note that pandas will auto-convert Python standard lib datetime to it's built-in datatype. Only if you have a pd.Series of type datetime.date or datetime.time, the type won't be modified.