From hours to String

Question:

I have this df:

Index          Dates
  0     2017-01-01 23:30:00
  1     2017-01-12 22:30:00
  2     2017-01-20 13:35:00
  3     2017-01-21 14:25:00
  4     2017-01-28 22:30:00
  5     2017-08-01 13:00:00
  6     2017-09-26 09:39:00
  7     2017-10-08 06:40:00 
  8     2017-10-04 07:30:00
  9     2017-12-13 07:40:00
  10    2017-12-31 14:55:00

The purpose was that between the time ranges 5:00 to 11:59 a new df would be created with data that would say: morning. To achieve this I converted those hours to booleans:

hour_morning=(pd.to_datetime(df['Dates']).dt.strftime('%H:%M:%S').between('05:00:00','11:59:00'))

and then passed them to a list with "morning" str

text_morning=[str('morning') for x in hour_morning if x==True]

I have the error in the last line because it only returns ´morning´ string values, it is as if the ‘X’ ignored the ‘if’ condition. Why is this happening and how do i fix it?

Asked By: Strovic

||

Answers:

Do

text_morning=[str('morning') if x==True else 'not_morning' for x in hour_morning ]

You can also use np.where:

text_morning = np.where(hour_morning, 'morning', 'not morning')
Answered By: Quang Hoang

Given:

                  Dates  values
0   2017-01-01 23:30:00       0
1   2017-01-12 22:30:00       1
2   2017-01-20 13:35:00       2
3   2017-01-21 14:25:00       3
4   2017-01-28 22:30:00       4
5   2017-08-01 13:00:00       5
6   2017-09-26 09:39:00       6
7   2017-10-08 06:40:00       7
8   2017-10-04 07:30:00       8
9   2017-12-13 07:40:00       9
10  2017-12-31 14:55:00      10

Doing:

# df.Dates = pd.to_datetime(df.Dates)
df = df.set_index("Dates")

Now we can use pd.DataFrame.between_time:

new_df = df.between_time('05:00:00','11:59:00')
print(new_df)

Output:

                     values
Dates
2017-09-26 09:39:00       6
2017-10-08 06:40:00       7
2017-10-04 07:30:00       8
2017-12-13 07:40:00       9

Or use it to update the original dataframe:

df.loc[df.between_time('05:00:00','11:59:00').index, 'morning'] = 'morning'

# Output:

                     values  morning
Dates
2017-01-01 23:30:00       0      NaN
2017-01-12 22:30:00       1      NaN
2017-01-20 13:35:00       2      NaN
2017-01-21 14:25:00       3      NaN
2017-01-28 22:30:00       4      NaN
2017-08-01 13:00:00       5      NaN
2017-09-26 09:39:00       6  morning
2017-10-08 06:40:00       7  morning
2017-10-04 07:30:00       8  morning
2017-12-13 07:40:00       9  morning
2017-12-31 14:55:00      10      NaN
Answered By: BeRT2me
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.