pang2016 pang2016 - 14 days ago 5
Python Question

get special date columns that columns contain date,hour,seconds

I have a dataframe:

df=pandas.DataFrame([{'remarkTime':'2014-03-03 10:18','name':'a'},{'remarkTime':'2015-05-03 09:12','name':'b'},
{'remarkTime':'2014-03-03 18:12','name':'c'}])


I want get the remarkTime which is 2014-03-03. the result like this:

name remarkTime
0 a 2014-03-03
1 c 2014-03-03


firstly,I try use "==" or "in" to get the row. but the "==" isn't suitable for this situation.secondly, I try split the "2014-03-03 10:18". but It failed.

Thanks a lot!

Answer

I think you can use boolean indexing with comparing date with date. Last parse date:

#convert to datetime if necessary
df.remarkTime = pd.to_datetime(df.remarkTime)

print (df.remarkTime.dt.date == pd.Timestamp(2014,3,3).date())
0     True
1    False
2     True
Name: remarkTime, dtype: bool

df = df[df.remarkTime.dt.date == pd.Timestamp(2014,3,3).date()]
print (df)
  name          remarkTime
0    a 2014-03-03 10:18:00
2    c 2014-03-03 18:12:00

df.remarkTime = df.remarkTime.dt.date
print (df)
  name  remarkTime
0    a  2014-03-03
2    c  2014-03-03