Reputation: 39
I'm just trying to convert a column to datetime object while loading a CSV file in Jupiter notebooks. Below is the code which I used.
d_parser=lambda x:pd.datetime.strptime(x,'%Y-%m-%d %I-%p')\
df2=pd.read_csv(r'C:\Users\Lenovo\Desktop\Jupyter Notebooks\eth_1h.csv',parse_dates=['Date'],date_parser=d_parser)
I got the below error:
FutureWarning: The pandas.datetime
class is deprecated and will be removed from pandas in a future version. Import from datetime module instead.
I got an idea that we have to import datetime
module. But Im not sure what changes I have to make in my existing code.
Upvotes: 0
Views: 1956
Reputation: 447
As of today, pandas is deprecating date_parser
in place of date_format
, so here is a (near)future-proof solution on the line of Pierre D's.
df = pd.read_csv(r'C:\...\eth_1h.csv', parse_dates=['Date'], date_format='%Y-%m-%d %I-%p')
Upvotes: 0
Reputation: 321
I'm following the same tutorials on YouTube by Corey Schafer since the csv file you were trying to read has the same file name as in the tutorial.
The warning actually provided the solution for you already, here is what I did:
from datetime import datetime
then change the d_parser to
d_parser = lambda x: datetime.strptime(x, '%Y-%m-%d %I-%p')
The warning should be gone.
Upvotes: 0
Reputation: 26271
Use to_datetime instead. Also a function will be clearer:
def d_parser(s):
return pd.to_datetime(s, format='%Y-%m-%d %I-%p')
df2 = pd.read_csv(r'C:\Users\...\eth_1h.csv', parse_dates=['Date'],
date_parser=d_parser)
You can also handle errors e.g. pd.to_datetime(..., error='coerce')
.
Upvotes: 2