python - How to format a date in a Pandas dataframe column with a certain date format such as YYYY-MMM-DD -
i able convert date value pandas datetime object, not able specify format of date without there being error.
the 2 formats want use yyyy-mmm-dd
format='%y%b%d'
, yyyy-mm-dd
format='%y%m%d'
however when try , use following code, error:
def convert_date(x): x = x.strip() x = pd.to_datetime(x,format='%y%b%d') return x
you can try:
d1 = ' 2015sep03 ' d2 = ' 2015-sep-03 ' d3 = ' 20150903' d4 = ' 2015-09-03' print pd.to_datetime(d1.strip(),format='%y%b%d') print pd.to_datetime(d2.strip(),format='%y-%b-%d') print pd.to_datetime(d2.strip()) print pd.to_datetime(d3.strip(),format='%y%m%d') print pd.to_datetime(d4.strip(),format='%y-%m-%d') print pd.to_datetime(d4.strip()) 2015-09-03 00:00:00 2015-09-03 00:00:00 2015-09-03 00:00:00 2015-09-03 00:00:00 2015-09-03 00:00:00 2015-09-03 00:00:00
Comments
Post a Comment