Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

python - Remove time portion of DateTime index in pandas

When I query a service through their API for daily data, they throw in a time portion which is equal to whatever time the query was made. So my pandas dataframe looks like this when I called the function at 14:54:36 -

2018-05-16 14:54:36  1024.75  1008.25      ...        39221        242897
2018-05-17 14:54:36  1017.00  1002.00      ...        35361        241132
2018-05-18 14:54:36  1015.75  1002.75      ...        49090        242938
2018-05-21 14:54:36  1034.50  1020.75      ...        56950        243316
2018-05-22 14:54:36  1043.75  1028.50      ...        49724        247874
2018-05-23 14:54:36  1049.00  1036.25      ...        46256        253609
2018-05-24 14:54:36  1059.75  1047.00      ...        65352        259617

As this is daily data, the time portion is useless. When I do:

data = pd.read_csv(StringIO(data), index_col=0, header=None,names=['High','Low','Open','Close','Volume','OpenInterest'])
data.index = pd.to_datetime(data.index,format="%Y-%m-%d")

The format doesn't seem to work. The DateTime index still contains time. Any idea how I can remove the time portion?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With the date attribute:

df.index = df.index.date

Example:

>>> df = pd.DataFrame([1, 2, 3, 4], index=pd.date_range('2018', periods=4, freq='H'))
>>> df.index = df.index.date
>>> df
            0
2018-01-01  1
2018-01-01  2
2018-01-01  3
2018-01-01  4

Note: that this will get you object dtype in Pandas. All attributes are here. It's technically an array of native Python datetime.date objects. See ALollz's answer to keep the dtype datetime-like.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...