“This is the 26th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
The datetime module is not included in the PANDAS library.
In order to learn the time sequences in pandas, we can learn the basic usage of datetime. datetime.date(), datetime.datetime(), datetime.timedelta()
datetime.date
We can use the datetime.date.today method to return the current time of datetime.date
import datetime
today = datetime.date.today()
print(today,type(today))
>>>
2018-10-28 <class 'datetime.date'>
Copy the code
We can also use datetime.date(year, month, day) to print the datetime type of the date we want.
import datetime
t = datetime.date(2016.6.1)
print(t)
# (year, month, day) → Get the current date directly
>>>
2016- 06-01Copy the code
datetime.datetime
We use the datetime.datetime.now method to return the current time, datetime.datetime
import datetime
now = datetime.datetime.now()
print(now,type(now))
>>>
2018-10-28 17:34:07.467642 <class 'datetime.datetime'>
Copy the code
We can also use datetime.datetime(year, month, day, hour, minute, second) to output the desired datetime.datetime type.
t1 = datetime.datetime(2016.6.1)
t2 = datetime.datetime(2014.1.1.12.44.33)
print(t1,t2)
>>>
2016- 06-0100:00:00 2014- 01-0112:44:33
Copy the code
datetime.timedelta
Datetime. timedelta represents data of the time difference data type. The time difference is mainly used as the addition and subtraction of time and is equivalent to the discernable time “difference”.
# datetime.timedelta: time difference
today = datetime.datetime.today() Datetime also has the today() method
yestoday = today - datetime.timedelta(1) #
print(today)
print(yestoday)
print(today - datetime.timedelta(7)) > > >2018-11- 0414:48:28.967149
2018-11- 0314:48:28.967149
2018-10-28 14:48:28.967149
Copy the code
Date parsing: parser.parse
In addition to the above conversion method, we need other methods for parsing strings into time format.
A string can be converted to datetime.datetime data using the parse method.
from dateutil.parser import parse
date = '12-21-2017'
t = parse(date)
print(t,type(t))
>>>
2017-12-21 00:00:00 <class 'datetime.datetime'>
Copy the code
Similarly, we can convert strings of other formats to datetime.datetime, but cannot parse Chinese
print(parse('2000-1-1'),'\n',
parse('5/1/2014'),'\n',
parse('5/1/2014', dayfirst = True),'\n'.# International format, the day before the month, can be set by dayFirst
parse('22/1/2014'),'\n',
parse('Jan 31, 1997 10:45 PM')) > > >2000- 01-0100:00:00
2014- 05-0200:00:00
2014- 01-0500:00:00
2014- 01 -22 00:00:00
1997- 01 -31 22:45:00
Copy the code
Pandas Indicates the Timestamp
- Time data represents a point in time and is the data type of pandas
- Is the most basic type of time series data that associates values with points in time.
Timestamp is basically the same as datetime, except for the pandas module and the Datetime module.
pandas.Timestape
Pandas.Timestape Generates time data for pandas
import numpy as np
import pandas as pd
date1 = datetime.datetime(2016.12.1.12.45.30) # create a datetime.datetime
date2 = '2017-12-21' Create a string
t1 = pd.Timestamp(date1)
t2 = pd.Timestamp(date2)
print(t1,type(t1))
print(t2,type(t2))
>>>
2016-12- 0112:45:30 <class 'pandas._libs.tslibs.timestamps.Timestamp'>
2017-12-21 00:00:00 <class 'pandas._libs.tslibs.timestamps.Timestamp'>
Copy the code
pandas.to_datetime
Pandas. To_datetime Converts the pandas’ single time data to the PANDAS ‘Timestamp data, and converts the pandas’ multiple time data to the PANDAS’ DatetimeIndex.
Single time data instance:
from datetime import datetime
date1 = datetime(2016.12.1.12.45.30)
date2 = '2017-12-21'
t1 = pd.to_datetime(date1)
t2 = pd.to_datetime(date2)
print(t1,type(t1))
print(t2,type(t2))
>>>
2016-12- 0112:45:30 <class 'pandas._libs.tslibs.timestamps.Timestamp'>
2017-12-21 00:00:00 <class 'pandas._libs.tslibs.timestamps.Timestamp'>
Copy the code
Multiple time data instances:
lst_date = [ '2017-12-21'.'2017-12-22'.'2017-12-23']
t3 = pd.to_datetime(lst_date)
print(t3,type(t3))
>>>
DatetimeIndex(['2017-12-21'.'2017-12-22'.'2017-12-23'], dtype='datetime64[ns]', freq=None) <class 'pandas.core.indexes.datetimes.DatetimeIndex'>
Copy the code
When there are other data formats in multiple time series, we can return with the error argument. Errors = ‘ignore’: returns the original input if it is not parsed
date3 = ['2017-2-1'.'2017-2-2'.'2017-2-3'.'hello world! '.'2017-2-5'.'2017-2-6']
t3 = pd.to_datetime(date3, errors = 'ignore')
print(t3,type(t3))
>>>
['2017-2-1' '2017-2-2' '2017-2-3' 'hello world! ' '2017-2-5' '2017-2-6'] <class 'numpy.ndarray'>
Copy the code
When errors = ‘COERce’, NaT will be returned for data of different data types as DatetimeIndex
date3 = ['2017-2-1'.'2017-2-2'.'2017-2-3'.'hello world! '.'2017-2-5'.'2017-2-6']
t4 = pd.to_datetime(date3, errors = 'coerce')
print(t4,type(t4))
>>>
DatetimeIndex(['2017-02-01'.'2017-02-02'.'2017-02-03'.'NaT'.'2017-02-05'.'2017-02-06'],
dtype='datetime64[ns]', freq=None) <class 'pandas.core.indexes.datetimes.DatetimeIndex'>
Copy the code