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.
#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


Messages In This Thread
RE: [Numpy] Load date/time from .txt to 'datetime64' type. - by Gribouillis - Mar-01-2024, 11:16 PM

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