MUD
MUD

Reputation: 121

Convert data from months, days, hours, minutes, into just minutes?

I have a .csv file of data in Months/Days/Hours/Minutes, and I need to convert it into just minutes. I can convert days and hours fine, but I am struggling with how to convert the months into minutes as they are not a standard length.

This is a snapshot of my data, the full file is too large to post here.

enter image description here

This is my code so far. It reads the file and computes the time in minutes correctly from the Day, Hour, and Minute columns. I can't think how to make it accurately calculate the number of minutes in each month - my rough idea was to define how many days in a month, then read which month it is from the data and use this as an index to cross-reference this with the month lengths variable and multiply that by 1440 (number of minutes in a day). But I don't know how to make Python do this?

# Import packages
import pandas as pd

# Open  data file
data_file_name = "data_file.csv"
data_file = pd.read_csv(data_file_name, header=2)

# Extract DHI solar data
solar_data = data_file["DHI"]

# Extract time points
# Define number of days in each month
month_lengths = [0,31,28,31,30,31,30,31,31,30,31,30,31] # Always one month out of sync

time_data = ((data_file["Day"]-1)*1440) + (data_file["Hour"]*60) + data_file["Minute"]

Upvotes: 1

Views: 867

Answers (1)

Mark Ransom
Mark Ransom

Reputation: 308432

Python has full calendar support in the datetime module. Use it.

import datetime
dt = datetime.datetime(data_file["Year"], data_file["Month"], data_file["Day"], data_file["Hour"] data_file["Minute"])
delta = dt - datetime.datetime(data_file["Year"], 1, 1)
time_data = delta.total_seconds() // 60

This has the advantage of automatically adjusting for leap years too.

Upvotes: 2

Related Questions