Mikhail Elizarev
Mikhail Elizarev

Reputation: 975

DatetimeIndex with time part only: is it possible

I've stuck with such a problem. I have a set of observation of passenger traffic. Data is stored in .xlsx file with the following structure: date_of_observation, time, station_name, boarding, alighting.

I wonder if it's possible to create Dataframe with DatetimeIndex from such data if I need only 'time' component of datetime. (No dublicates of time is presented in dataset).

The reason for this requirement is that I use specific logic based on circular time (for example, 23.00 < 0.00, but 0.01 < 0.02 when compared), so I don't want to convert them to datetime.

Upvotes: 1

Views: 1566

Answers (2)

Mikhail Elizarev
Mikhail Elizarev

Reputation: 975

No, it is not possible, only with datetime or with float index. However, variant offered by unutbu is very useful.

Upvotes: 0

unutbu
unutbu

Reputation: 880399

Perhaps you do not need to reduce the DatetimeIndex to just a time. Instead, to select rows based solely on the time component, you could use DataFrame.between_time. For example,

import pandas as pd
import numpy as np

N = 200
dti = pd.date_range('2000-1-1', freq='10T', periods=N)
df = pd.DataFrame({'station_name': np.random.choice(list('ABCDEFGHIJ'), size=N),
                   'boarding': np.arange(N)*10,
                   'alighting': np.arange(N)},
                  index=dti)

The dataframe looks like this:

>>> print(df.head())

                     alighting  boarding station_name
2000-01-01 00:00:00          0         0            B
2000-01-01 00:10:00          1        10            I
2000-01-01 00:20:00          2        20            H
2000-01-01 00:30:00          3        30            C
2000-01-01 00:40:00          4        40            E

But you can select all the rows whose times are between 23:00 and 0:30 like this:

>>> print(df.between_time('23:00', '0:30'))
                     alighting  boarding station_name
2000-01-01 00:00:00          0         0            B
2000-01-01 00:10:00          1        10            I
2000-01-01 00:20:00          2        20            H
2000-01-01 00:30:00          3        30            C
2000-01-01 23:00:00        138      1380            D
2000-01-01 23:10:00        139      1390            E
2000-01-01 23:20:00        140      1400            A
2000-01-01 23:30:00        141      1410            D
2000-01-01 23:40:00        142      1420            E
2000-01-01 23:50:00        143      1430            B
2000-01-02 00:00:00        144      1440            B
2000-01-02 00:10:00        145      1450            I
2000-01-02 00:20:00        146      1460            F
2000-01-02 00:30:00        147      1470            C

Upvotes: 3

Related Questions