Anyone please help me in changing time format in python. I have two files (One from supermag website, another from Themisa spacecraft data). Both have different time format.
Fist file has format:
<year> <month> <day> <hour> <min> <sec>
2015 01 01 00 00 00
2015 01 01 00 01 00
2015 01 01 00 02 00
2015 01 01 00 03 00
2015 01 01 00 04 00
2015 01 01 00 04 00
2015 01 01 00 06 00
.
.
.
few hundred thousand records```
Second file has format:
yyyy ddd hh:mm:ss hh:mm:ss
2015 1 00:00:00 17:56:21
2015 1 00:01:00 17:56:35
2015 1 00:02:00 17:56:49
2015 1 00:03:00 17:57:03
2015 1 00:04:00 17:57:17
2015 1 00:05:00 17:57:30
2015 1 00:06:00 17:57:44
2015 1 00:07:00 17:57:58
2015 1 00:08:00 17:58:11
.
.
.
few hundred thousand records```
Anyone please help me how to change time format of both files in UTC format so that I could map their date_time column for further analysis.
Thanking you
CodePudding user response:
You can turn these string to dates like this
import pandas as pd
import datetime
df = pd.read_csv("file.txt", sep='\t', lineterminator='\n', index_col=False)
values = [i[0].split(" ") for i in df.values]
list_of_dates, dates = [], []
for i in values:
dates = []
# Parsing Dates
for date in i:
if len(date.strip())>0:dates.append(date.strip())
dates = str(datetime.datetime.strptime("-".join(dates), '%Y-%m-%d-%H-%M-%S'))
list_of_dates.append(dates)
list_of_dates
CodePudding user response:
In [43]: foo = '''2015 01 01 00 00 00
...: 2015 01 01 00 01 00
...: 2015 01 01 00 02 00
...: 2015 01 01 00 03 00
...: 2015 01 01 00 04 00
...: 2015 01 01 00 04 00
...: 2015 01 01 00 06 00'''
In [44]: [datetime.datetime(*map(int, f.split())).isoformat() for f in foo.splitlines()]
Out[44]:
['2015-01-01T00:00:00',
'2015-01-01T00:01:00',
'2015-01-01T00:02:00',
'2015-01-01T00:03:00',
'2015-01-01T00:04:00',
'2015-01-01T00:04:00',
'2015-01-01T00:06:00']
In a slightly more readable version:
from datetime import datetime, timezone
def convert_format_one(line):
date_str_parts = line.split()
date_int_parts = [int(x) for x in date_str_parts]
date_obj = datetime(*date_int_parts, tzinfo=timezone.utc)
return date_obj