python - Changing the format of a column of Data frame from Str to Date in specific format -


i have 2 data frames have merge on date. type of data isn't same. date , of str format.

print(visit_data.iloc[0]['visit_date']) 2016-05-22  type(visit_data.iloc[0]['visit_date']) out[40]: datetime.date  print(holiday_data.iloc[0]['visit_date']) 1/1/2016  type(holiday_data.iloc[0]['visit_date']) out[46]: str 

so type , formats. using merge.

    data_store = pd.merge(data_store, holiday_data, how = 'left' , on = ['visit_date','state']).reset_index(drop=true) 

merge happening there 2 keys data not coming in output due different format of visit_date. so, tried change this.

holiday_data['visit_date'] = pd.to_datetime(holiday_data['visit_date'], format = 'format="%m/%d/%y') 

but gives

print(holiday_data.iloc[0]['visit_date']) 2016-01-01 00:00:00  type(holiday_data.iloc[0]['visit_date']) out[54]: pandas.tslib.timestamp 

which not want.

i changing data type of holiday_data because length of data_store long.so on resolve issue? using python2.7

if want return datetime object, this:

import datetime   holiday_data['visit_date'] = holiday_data['visit_date'].apply(lambda x:                                      datetime.datetime.strptime(x,'%m/%d/%y')) 

edit :

to retrieve date datetime object, use dt accessor.

working example:

in [2]: df = pd.dataframe(['1/1/2016', '1/2/2016', '1/3/2016', '1/4/2016', '1/5/2016'], columns=['mycol'])  in [3]: df['mycol'] = df['mycol'].apply(lambda x: datetime.datetime.strptime(x,'%m/%d/%y')).dt.date  in [4]: df out[4]:          mycol 0  2016-01-01 1  2016-01-02 2  2016-01-03 3  2016-01-04 4  2016-01-05  in [5]: df.iloc[0]['mycol'] out[5]: datetime.date(2016, 1, 1)  in [6]: type(df.iloc[0]['mycol']) out[6]: datetime.date 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -