TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Pandas Datetime

Pandas Datetime with What is Python Pandas, Reading Multiple Files, Null values, Multiple index, Application, Application Basics, Resampling, Plotting the data, Moving windows functions, Series, Read the file, Data operations, Filter Data etc.

<< Back to PANDAS

Pandas Datetime

The Pandas can provide the features to work with time-series data for all domains. It also consolidates a large number of features from other Python libraries like scikits.timeseries by using the NumPy datetime64 and timedelta64 dtypes. It provides new functionalities for manipulating the time series data.

The time series tools are most useful for data science applications and deals with other packages used in Python.

Example1:

import pandas as pd   
# Create the dates with frequency   
info = pd.date_range('5/4/2013', periods = 8, freq ='S')   
info

Output:

DatetimeIndex(['2013-05-04 00:00:00', '2013-05-04 00:00:01',
               '2013-05-04 00:00:02', '2013-05-04 00:00:03',
               '2013-05-04 00:00:04', '2013-05-04 00:00:05',
               '2013-05-04 00:00:06', '2013-05-04 00:00:07'],
              dtype='datetime64[ns]', freq='S')

Example2:

info = pd.DataFrame({'year': [2014, 2012],
'month': [5, 7],
'day': [20, 17]})
pd.to_datetime(info)
0   2014-05-20
1   2012-07-17
dtype: datetime64[ns]

You can pass errors='ignore' if the date does not meet the timestamp. It will return the original input without raising any exception.

If you pass errors='coerce', it will force an out-of-bounds date to NaT.

import pandas as pd
pd.to_datetime('18000706', format='%Y%m%d', errors='ignore')
datetime.datetime(1800, 7, 6, 0, 0)
pd.to_datetime('18000706', format='%Y%m%d', errors='coerce')

Output:

Timestamp('1800-07-06 00:00:00')

Example3:

import pandas as pd
dmy = pd.date_range('2017-06-04', periods=5, freq='S')
dmy

Output:

DatetimeIndex(['2017-06-04 00:00:00', 
               '2017-06-04 00:00:01',
               '2017-06-04 00:00:02',
               '2017-06-04 00:00:03',
               '2017-06-04 00:00:04'],
               dtype='datetime64[ns]', freq='S')

Example4:

import pandas as pd
dmy = dmy.tz_localize('UTC')
dmy

Output:

DatetimeIndex(['2017-06-04 00:00:00+00:00', '2017-06-04 00:00:01+00:00',
               '2017-06-04 00:00:02+00:00', 
               '2017-06-04 00:00:03+00:00',
               '2017-06-04 00:00:04+00:00'],
              dtype='datetime64[ns, UTC]', freq='S')

Example5:

import pandas as pd
dmy = pd.date_range('2017-06-04', periods=5, freq='S')
dmy

Output:

DatetimeIndex(['2017-06-04 00:00:00', '2017-06-04 00:00:01',
               '2017-06-04 00:00:02', '2017-06-04 00:00:03',
               '2017-06-04 00:00:04'],
        dtype='datetime64[ns]', freq='S')

Next TopicPandas Time Offset




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf