dinsdag 30 juni 2020

date and time python


date en time data voorkomens

  • Time stamps reference particular moments in time (e.g., July 4th, 2015 at 7:00am).
  • Time intervals and periods reference a length of time between a particular beginning and end point; for example, the year 2015. Periods usually reference a special case of time intervals in which each interval is of uniform length and does not overlap (e.g., 24 hour-long periods comprising days).
  • Time deltas or durations reference an exact length of time (e.g., a duration of 22.56 seconds).


DATE AND TIMES IN PYTHON

Er zijn verschillende packages in python mbt times, dates , deltas

native python dates en times: packages Datetime and dateutil

handig voor veel zaken zoals het opbouwen van datetime, dates en conversies.

niet handig als when you wish to work with large arrays of dates and times: just as lists of Python numerical variables are suboptimal compared to NumPy-style typed numerical arrays, lists of Python datetime objects are suboptimal compared to typed arrays of encoded dates.

typed arrays of times: NumPy's datetime64

The weaknesses of Python's datetime format inspired the NumPy team to add a set of 
native time series data type to NumPy. The datetime64 dtype encodes dates as 64-bit integers, 
and thus allows arrays of dates to be represented very compactly.
 
One detail of the datetime64 and timedelta64 objects is that they are built on a 
fundamental time unit. Because the datetime64 object is limited to 64-bit precision, 
the range of encodable times is 264 times this fundamental unit. In other words, 
datetime64 imposes a trade-off between time resolution and maximum time span. 
 Anderd nadeel dat het niet zo handig werkt als datetime and dateutil
 

Pandas dates and times Beste alternatief

Pandas builds upon all the tools just discussed to provide a Timestamp object, which combines the ease-of-use of datetime and dateutil with the efficient storage and vectorized interface of numpy.datetime64. From a group of these Timestamp objects, Pandas can construct a DatetimeIndex that can be used to index data in a Series or DataFrame; we'll see many examples of this below.

For example, we can use Pandas tools to repeat the demonstration from above. We can parse a flexibly formatted string date, and use format codes to output the day of the week:

import pandas as pd
date = pd.to_datetime("4th of July, 2015")
date 
>Timestamp('2015-07-04 00:00:00')
 
date.strftime('%A')
>'Saturday'

Additionally, we can do NumPy-style vectorized operations directly on this same object:
date + pd.to_timedelta(np.arange(12), 'D')
>DatetimeIndex(['2015-07-04', '2015-07-05', '2015-07-06', '2015-07-07',
               '2015-07-08', '2015-07-09', '2015-07-10', '2015-07-11',
               '2015-07-12', '2015-07-13', '2015-07-14', '2015-07-15'],
              dtype='datetime64[ns]', freq=None)


 

Geen opmerkingen:

Een reactie posten

Datums bepalen adhv begin en einddatum in Dataframe

Voorbeeld op losse velden  ####################################################################### # import necessary packages from datetime...