File dataexample_df.txt:
2020-12-04_163024 26.15 26.37 19.40 24.57
2020-12-04_163026 26.15 26.37 19.20 24.57
2020-12-04_163028 26.05 26.37 18.78 24.57
I want to read it in as pandas dataframe where the index column has only the time part in format '%H:%M:%S', without the date.
import pandas as pd
df = pd.read_csv("dataexample_df.txt", sep=' ', header=None, index_col=0)
print(df)
Output:
1 2 3 4
0
2020-12-04_163024 26.15 26.37 19.40 24.57
2020-12-04_163026 26.15 26.37 19.20 24.57
2020-12-04_163028 26.05 26.37 18.78 24.57
However, wanted output:
1 2 3 4
0
16:30:24 26.15 26.37 19.40 24.57
16:30:26 26.15 26.37 19.20 24.57
16:30:28 26.05 26.37 18.78 24.57
I have tried different date_parser= -functions (cf. Answers in Parse_dates in Pandas)
but get only error messages. Also, somewhat relevant is Python/Pandas convert string to time only but no luck, I'm stuck. I'm using Python 3.7.
Considering your df to be this:
In [121]: df
Out[121]:
1 2 3 4
0
2020-12-04_163024 26.15 26.37 19.40 24.57
2020-12-04_163026 26.15 26.37 19.20 24.57
2020-12-04_163028 26.05 26.37 18.78 24.57
You can use Series.replace with Series.dt.time:
In [122]: df.reset_index(inplace=True)
In [127]: df[0] = pd.to_datetime(df[0].str.replace('_', ' ')).dt.time
In [130]: df.set_index(0, inplace=True)
In [131]: df
Out[131]:
1 2 3 4
0
16:30:24 26.15 26.37 19.40 24.57
16:30:26 26.15 26.37 19.20 24.57
16:30:28 26.05 26.37 18.78 24.57
Here, I created a simple function to formate your datetime column, Please try this.
import pandas as pd
df = pd.read_csv('data.txt', sep=" ", header=None)
def format_time(date_str):
# split date and time
time = iter(date_str.split('_')[1])
# retun the time value adding
return ':'.join(a+b for a,b in zip(time, time))
df[0] = df[0].apply(format_time)
print(df)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With