Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to convert Matlab Timestamp to datetime in Python

I am trying to convert timestamp column to datetime. This is part of my data set:

Time,INDOOR Ambient Temp.,INDOOR Relative Humidity,INDOOR Air Velocity,INDOOR Mean Radiant Temp.,INDOOR Lumens,INDOOR CO2,Predicted Mean Vote (PMV)
735080.010417,24.584695,63.70399999999999,0.030988,24.584695,,-0.269505
735080.020833,24.584695,63.856,0.030988,24.584695,,-0.26837300000000003

When parsing to datetime using the following code:

# Load data
df = pd.read_csv("ContData.txt", parse_dates=['Time'])

# Group by day and compute the max temp per day
df.index = df['Time']

pd.to_datetime(df['Time']).apply(lambda x: x.date())

# Identify the day, month and year
df['day'] = df['Time'].map(lambda x: x.day)
df['month'] = df['Time'].map(lambda x: x.month)
df['year'] = df['Time'].map(lambda x: x.year)

I am getting the following error:

ValueError: hour must be in 0..23
like image 239
M Khalil Avatar asked Apr 30 '26 22:04

M Khalil


1 Answers

Matlab considers the origin January 0, 0000 and outputs the date as the number of days since then. This creates a bit of an issue because that's not a real date and well outside of the datetime64[ns] bounds. With a simple subtraction relative to the POSIX origin (1970-01-01) you can then use the vectorized pd.to_datetime conversion.

import pandas as pd
from datetime import datetime

# Additional 366 because January 0, year 0000 
offset = datetime(1970, 1, 1).toordinal() + 366  #719529

pd.to_datetime(df['Time']-offset, unit='D')
#0   2012-07-30 00:15:00.028799999
#1   2012-07-30 00:29:59.971200000
#Name: Time, dtype: datetime64[ns]
like image 199
ALollz Avatar answered May 02 '26 10:05

ALollz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!