Series 11 Datetime methods

In this lesson, you will learn about Datetime in Pandas-Series.

Datetime method

  • Series.dt.to_period()
  • Series.dt.to_pydatetime()
  • Series.dt.tz_localize()
  • Series.dt.tz_convert()
  • Series.dt.normalize()
  • Series.dt.strftime()
  • Series.dt.round()
  • Series.dt.floor()
  • Series.dt.ceil()
  • Series.dt.month_name()
  • Series.dt.day_name()

Detailed introduction

Start by importing the required dependency packages

In [1] :import numpy as np                                                               
In [2] :import pandas as pd
Copy the code

1. Series.dt.to_period()

Series.dt.to_period(*args, **kwargs)
Copy the code

Convert DatetimeArray/Index to PeriodArray/Index(at a specific frequency).

Common parameters:
  • Freq: STR or Offset, optional
In [7]: index=pd.to_datetime(["The 2000-03-31 00:00:00"."The 2000-05-31 00:00:00"."The 2000-08-31 00:00:00"])                                                     
In [8]: index                                                                   
Out[8]: DatetimeIndex(['2000-03-31'.'2000-05-31'.'2000-08-31'], dtype='datetime64[ns]', freq=None)

You can observe that the DType changes
In [9]: index.to_period("M")                                                    
Out[9]: PeriodIndex(['the 2000-03'.'the 2000-05'.'the 2000-08'], dtype='period[M]', freq='M')   

# if no parameter is specified, automatic inference is performed
# note ⚠️ : IDx can be inferred automatically because idx has a value for freq, while index cannot be inferred automatically because index has a freq attribute of None
In [10]: idx = pd.date_range("2017-01-01", periods=2)               
In [11]: idx                                                                    
Out[11]: DatetimeIndex(['2017-01-01'.'2017-01-02'], dtype='datetime64[ns]', freq='D')

In [12]: idx.to_period()                                                        
Out[12]: PeriodIndex(['2017-01-01'.'2017-01-02'], dtype='period[D]', freq='D')
Copy the code

2. Series.dt.to_pydatetime()

Series.dt.to_pydatetime()
Copy the code

Returns an array of python-datetime objects. Python’s date times use microsecond resolution, lower than pandas (nanoseconds), and the value will be truncated.

In [17]: s = pd.Series(pd.date_range('20180310', periods=2, freq='ns'))         
In [18]: s                                                                      
Out[18] :0   2018- 03 -10 00:00:00.000000000
1   2018- 03 -10 00:00:00.000000001
dtype: datetime64[ns]

In [19]: s.dt.to_pydatetime()                                                   
Out[19]: 
array([datetime.datetime(2018.3.10.0.0),
       datetime.datetime(2018.3.10.0.0)], dtype=object)
Copy the code

3. Series.dt.tz_localize()

Series.dt.tz_localize(*args, **kwargs)
Copy the code

Time zone localization, which was introduced in Lecture 9 (Series).

4. Series.dt.tz_convert()

Series.dt.tz_convert(*args, **kwargs)
Copy the code

Time zone conversion, which was introduced in Lecture 9 (time-dependent Series).

5. Series.dt.normalize()

Series.dt.normalize(*args, **kwargs)
Copy the code

The time portion of the date time will be converted to midnight, which is 00:00:00. This is useful in situations where time doesn’t matter. The length doesn’t change. Time zones are not affected.

In [20]: idx = pd.date_range(start='the 2014-08-01 a.m., freq='H', periods=3, tz='Asia/Calcutta')                                         
In [21]: idx                                                                    
Out[21]: 
DatetimeIndex(['the 2014-08-01 10:00:00 + 05:30'.'the 2014-08-01 11:00:00 + 05:30'.'the 2014-08-01 12:00:00 + 05:30'],
              dtype='datetime64[ns, Asia/Calcutta]', freq='H')

In [22]: idx.normalize()                                                        
Out[22]: 
DatetimeIndex(['2014-08-01 00:00:00 + 05:30'.'2014-08-01 00:00:00 + 05:30'.'2014-08-01 00:00:00 + 05:30'],
              dtype='datetime64[ns, Asia/Calcutta]', freq=None)
Copy the code

6. Series.dt.strftime()

Series.dt.strftime(*args, **kwargs)
Copy the code

Returns an index of the formatted string specified by date_format that supports the same string format as the Python standard library.

Common parameters:
  • Date_format: STR [formatted time string, e.g. %Y-%m-%d]
In [23]: rng = pd.date_range(pd.Timestamp("The 2018-03-10 GMT"), periods=3, freq='s')                               

In [24]: rng                                                                    
Out[24]: 
DatetimeIndex(['the 2018-03-10 09:00:00'.'the 2018-03-10 09:00:01'.'the 2018-03-10 09:00:02'],
              dtype='datetime64[ns]', freq='S')

In [25]: rng.strftime('%B %d, %Y, %r')                                          
Out[25]: 
Index(['March 10, 2018, 09:00:00 AM'.'March 10, 2018, 09:00:01 AM'.'March 10, 2018, 09:00:02 AM'],
      dtype='object')
Copy the code

7. Series.dt.round()

Series.dt.round(*args, **kwargs)
Copy the code

Performs a round operation on data to the specified time frequency.

# DatetimeIndex
In [26]: rng = pd.date_range('1/1/2018 like', periods=3, freq='min')        
In [27]: rng                                                                    
Out[27]: 
DatetimeIndex(['2018-01-01 11:59:00'.'the 2018-01-01 12:00:00'.'the 2018-01-01 12:01:00'],
              dtype='datetime64[ns]', freq='T')

In [28]: rng.round('H')                                                         
Out[28]: 
DatetimeIndex(['the 2018-01-01 12:00:00'.'the 2018-01-01 12:00:00'.'the 2018-01-01 12:00:00'],
              dtype='datetime64[ns]', freq=None)

# Series
In [29]: pd.Series(rng).dt.round("H")                                           
Out[29] :0   2018- 01-0112:00:00
1   2018- 01-0112:00:00
2   2018- 01-0112:00:00
dtype: datetime64[ns]
Copy the code

8. Series.dt.floor()

Series.dt.floor(*args, **kwargs)
Copy the code

It’s just like round, except it’s going to be evaluated down.

In [30]: rng = pd.date_range('1/1/2018 like', periods=3, freq='min')        
In [31]: rng                                                                    
Out[31]: 
DatetimeIndex(['2018-01-01 11:59:00'.'the 2018-01-01 12:00:00'.'the 2018-01-01 12:01:00'],
              dtype='datetime64[ns]', freq='T')

In [32]: rng.floor('H')                                                         
Out[32]: 
DatetimeIndex(['the 2018-01-01 11:00:00'.'the 2018-01-01 12:00:00'.'the 2018-01-01 12:00:00'],
              dtype='datetime64[ns]', freq=None)
Copy the code

9. Series.dt.ceil()

Series.dt.ceil(*args, **kwargs)
Copy the code

It’s just like round, except it’s going to be evaluated up.

In [33]: rng.ceil('H')                                                          
Out[33]: 
DatetimeIndex(['the 2018-01-01 12:00:00'.'the 2018-01-01 12:00:00'.'the 2018-01-01 13:00:00'],
              dtype='datetime64[ns]', freq=None)
Copy the code

10. Series.dt.month_name()

Series.dt.month_name(*args, **kwargs)
Copy the code

Returns the name of the month with DateTimeIndex for the specified locale.

In [35]: rng.month_name()                                                       
Out[35]: Index(['January'.'January'.'January'], dtype='object')
Copy the code

11. Series.dt.day_name()

Series.dt.day_name(*args, **kwargs)
Copy the code

Returns the date name of the DateTimeIndex with the specified locale.

In [36]: rng.day_name()                                                         
Out[36]: Index(['Monday'.'Monday'.'Monday'], dtype='object')
Copy the code