Pandas date conversion with string to

I start with python and pandas and matplotlib. I work with data with over a million records. I am trying to change the date format. The CSV file format is 23-Jan-11. I would like to use future dates to determine the amount of donation for each candidate. How to convert date format to readable format for pandas?

Here is the link to cut the file 149 entries

My code is:

%matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

First candidate

reader_bachmann = pd.read_csv('P00000001-ALL.csv' ,converters={'cand_id': lambda x: str(x)[1:]},parse_dates=True, squeeze=True, low_memory=False, nrows=411 )

date_frame = pd.DataFrame(reader_bachmann, columns = ['contb_receipt_dt'])

data slice

  s = date_frame.iloc[:,0]
    date_slice = pd.Series([s])
    date_strip = date_slice.str.replace('JUN','6') 

Trying to convert to a new date format

 date = pd.to_datetime(s, format='%d%b%Y')
    print(date_slice)

Here is the error message

ValueError: could not convert string to float: '05-JUL-11'
+4
source share
2 answers

dateutil.parser , , .

l = [] 
for i in range(100):
    l.append('23-JUN-11') 
B = pd.DataFrame({'Date':l})

dateutil.parser

import dateutil.parser
B['Date2'] = B['Date'].apply(lambda x : dateutil.parser.parse(x))
B.head()
Out[106]: 
    Date      Date2
0  23-JUN-11 2011-06-23
1  23-JUN-11 2011-06-23
2  23-JUN-11 2011-06-23
3  23-JUN-11 2011-06-23
4  23-JUN-11 2011-06-23
+1

:

format='%d-%b-%y'

?

, :

ValueError: float: '05 -JUL-11 '

:

format='%d%b%Y'

:

%y - year without a century (range 00 to 99)
%b - abbreviated month name
%d - day of the month (01 to 31)

-, , y y .

+6

Source: https://habr.com/ru/post/1675284/


All Articles