Python Forum
[Numpy] Load date/time from .txt to 'datetime64' type.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Numpy] Load date/time from .txt to 'datetime64' type.
#1
I have date/time data in .txt file likes below format:
2024-2-27, 0:0:0
2024-2-27, 3:7:2
2024-2-27, 4:11:3
2024-2-27, 6:3:5
2024-2-27, 13:10:6
2024-2-27, 20:20:7
2024-2-27, 21:30:9
2024-2-27, 22:40:10
2024-2-27, 23:50:12
I want to load that use numpy.loadtxt() and convert to 'datetime64' type, but 'datetime64' type require string format likes below:
numpy.array(['2020-02-27T15:23:07'], dtype = 'datetime64[s]')
so I must fill some '0', and concatenate date and time use 'T'?

# fill '0'
2024-02-27, 00:00:00
2024-02-27, 03:07:02
2024-02-27, 04:11:03
2024-02-27, 06:03:05
2024-02-27, 13:10:06
2024-02-27, 20:20:07
2024-02-27, 21:30:09
2024-02-27, 22:40:10
2024-02-27, 23:50:12

# concatenate use 'T'
2024-02-27T00:00:00
2024-02-27T03:07:02
2024-02-27T04:11:03
2024-02-27T06:03:05
2024-02-27T13:10:06
2024-02-27T20:20:07
2024-02-27T21:30:09
2024-02-27T22:40:10
2024-02-27T23:50:12
Is it have simply way to do that?
Reply
#2
This may not be the fastest method but it seems to work
import datetime as dt
import io
import numpy as np

file = io.StringIO(
    """\
2024-2-27, 0:0:0
2024-2-27, 3:7:2
2024-2-27, 4:11:3
2024-2-27, 6:3:5
2024-2-27, 13:10:6
2024-2-27, 20:20:7
2024-2-27, 21:30:9
2024-2-27, 22:40:10
2024-2-27, 23:50:12
"""
)


def gen(file):
    for line in file:
        t = dt.datetime.strptime(line, "%Y-%m-%d, %H:%M:%S\n")
        yield t.isoformat()


a = np.loadtxt(gen(file), dtype="datetime64[s]")
print(a)
Output:
['2024-02-27T00:00:00' '2024-02-27T03:07:02' '2024-02-27T04:11:03' '2024-02-27T06:03:05' '2024-02-27T13:10:06' '2024-02-27T20:20:07' '2024-02-27T21:30:09' '2024-02-27T22:40:10' '2024-02-27T23:50:12']
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
(Mar-01-2024, 08:54 AM)water Wrote: I have date/time data in .txt file likes below format:
2024-2-27, 0:0:0
2024-2-27, 3:7:2
2024-2-27, 4:11:3
2024-2-27, 6:3:5
2024-2-27, 13:10:6
2024-2-27, 20:20:7
2024-2-27, 21:30:9
2024-2-27, 22:40:10
2024-2-27, 23:50:12
I want to load that use numpy.loadtxt() and convert to 'datetime64' type, but 'datetime64' type require string format likes below:
numpy.array(['2020-02-27T15:23:07'], dtype = 'datetime64[s]')
so I must fill some '0', and concatenate date and time use 'T'?

# fill '0'
2024-02-27, 00:00:00
2024-02-27, 03:07:02
2024-02-27, 04:11:03
2024-02-27, 06:03:05
2024-02-27, 13:10:06
2024-02-27, 20:20:07
2024-02-27, 21:30:09
2024-02-27, 22:40:10
2024-02-27, 23:50:12

# concatenate use 'T'
2024-02-27T00:00:00
2024-02-27T03:07:02
2024-02-27T04:11:03
2024-02-27T06:03:05
2024-02-27T13:10:06
2024-02-27T20:20:07
2024-02-27T21:30:09
2024-02-27T22:40:10
2024-02-27T23:50:12
Is it have simply way to do that?
You can achieve this by using the numpy.genfromtxt() function with a custom converter to parse your date/time data and convert it to datetime64 type.

import numpy as np

# Custom converter function to parse date/time strings and convert to datetime64
def datetime_converter(date_str, time_str):
datetime_str = f"{date_str.strip()}, {time_str.strip()}"
return np.datetime64(datetime_str)

# Load data from the text file using genfromtxt with the custom converter
data = np.genfromtxt('your_file.txt', delimiter=',', dtype='U', converters={0: lambda x: x, 1: lambda x: x}, unpack=True)

# Extract date and time columns
dates = data[0]
times = data[1]

# Convert to datetime64 using the custom converter
datetime_array = np.array([datetime_converter(date, time) for date, time in zip(dates, times)])

print(datetime_array)

i hope This code will load your data from the text file, parse the date and time strings, and convert them to datetime64 type using the custom converter function. The resulting datetime_array will contain the datetime values in the desired format.

Best regard
Danish hafeez | QA Assistant
buran write Mar-01-2024, 10:04 AM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Spam/advertisement link removed
Reply
#4
(Mar-01-2024, 09:30 AM)Gribouillis Wrote: def gen(file):
for line in file:
t = dt.datetime.strptime(line, "%Y-%m-%d, %H:%M:%S\n")
yield t.isoformat()

That seems a good method for this case, thanks.
Reply
#5
(Mar-01-2024, 07:25 PM)water Wrote: That seems a good method for this case, thanks.
You can skip a double conversion by creating the array with numpy.fromiter()
import datetime as dt
import io
import numpy as np

file = io.StringIO(
    """\
2024-2-27, 0:0:0
2024-2-27, 3:7:2
2024-2-27, 4:11:3
2024-2-27, 6:3:5
2024-2-27, 13:10:6
2024-2-27, 20:20:7
2024-2-27, 21:30:9
2024-2-27, 22:40:10
2024-2-27, 23:50:12
"""
)


def parse(line):
    return dt.datetime.strptime(line, "%Y-%m-%d, %H:%M:%S\n")


a = np.fromiter((parse(line) for line in file), dtype="datetime64[s]")

print(a)
« We can solve any problem by introducing an extra level of indirection »
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Numpy] How to store different data type in one numpy array? water 7 626 Mar-26-2024, 02:18 PM
Last Post: snippsat
  GroupBy - Sum = Error [datetime64 type does not support sum operations] BSDevo 4 2,902 Oct-27-2023, 07:22 PM
Last Post: BSDevo
  Pandas read csv file in 'date/time' chunks MorganSamage 4 1,721 Feb-13-2023, 11:24 AM
Last Post: MorganSamage
  Numpy returns "TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'int'" kalle 2 2,642 Jul-19-2022, 06:31 AM
Last Post: paul18fr
  Does a pandas have a date without a time? AlekseyPython 6 4,964 Feb-10-2021, 09:24 AM
Last Post: Naheed
  How to fill datetime64 field in numpy structured array? AlekseyPython 0 2,291 Oct-20-2020, 08:17 AM
Last Post: AlekseyPython
  How to prepare a NumPy array which include float type array elements subhash 0 1,927 Mar-02-2020, 06:46 AM
Last Post: subhash
  replace nan values by mean group by date.year, date.month wissam1974 5 8,506 Feb-19-2020, 06:25 PM
Last Post: AnkitGupta
  Problem with date type (object to datetime) karlito 6 3,620 Oct-16-2019, 08:07 AM
Last Post: karlito
  "erlarge" a numpy-matrix to numpy-array PhysChem 2 3,007 Apr-09-2019, 04:54 PM
Last Post: PhysChem

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020