3

C is a Data series with shape of (10000000, ) with dtypes of dtype(< M8[ns]). I want to create a dataseries which only contain one hour of C.

 c.between_time('22:00:00','23:00:00')

This is the error that I get

 TypeError: Index must be DatetimeIndex

How should I fix it?

3
  • you probably need to add the index to the series of type "DateTimeIndex" (and of course it becomes a dataframe), the index currently must be integers Commented Oct 31, 2015 at 15:23
  • 'Series' object has no attribute 'set_index' Commented Oct 31, 2015 at 15:28
  • convert it to a dataframe Commented Oct 31, 2015 at 15:29

1 Answer 1

4

Try creating a dataframe by initializing an empty dataframe.
I created a sample of 3 dummy times in Series

import pandas as pd  
C = pd.Series(['22:00:00','22:30:00','23:00:00'])

df = pd.DataFrame()
df['C'] = C

#set index to the obervations after converting them to type datetimeIndex
df.index = pd.to_datetime(C)
print df

print df.between_time(df['C'][0],df['C'][2])
Sign up to request clarification or add additional context in comments.

5 Comments

is this what you want?
Thanks, I have a quick question: some of my entries are in the form of 2015-10-05 02:21:36 and some in the form of 2015-10-05. Do you know how can I remove the second ones?
The output of this gives me NAN: df = df.ix[df.time.str.len() > 14]
What are the contents of the column called 'time' ?
I think you should ask a separate question for it as it is out of scope of the original question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.