TheDeveloperBlog.com

Home | Contact Us

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

Pandas Convert string to date

Pandas Convert string to date 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

Convert string to date

In today's time, it is a tedious task to analyze datasets with dates and times. Because of different lengths in months, distributions of the weekdays and weekends, leap years, and the time zones are the things that needs to consider according to our context. So, for this reason, Python has defined a new data type especially for dates and times called datetime.

However, in many datasets, Strings are used to represent the dates. So, in this topic, you'll learn about converting date strings to the datetime format and see how these powerful set of tools helps to work effectively with complicated time series data.

The challenge behind this scenario is how the date strings are expressed. For example, 'Wednesday, June 6, 2018' can also be shown as '6/6/18' and '06-06-2018'. All these formats define the same date, but the code represents to convert each of them is slightly different.

fromdatetime import datetime

# Define dates as the strings	
dmy_str1 = 'Wednesday, July 14, 2018'
dmy_str2 = '14/7/17'
dmy_str3 = '14-07-2017'

# Define dates as the datetime objects
dmy_dt1 = datetime.strptime(date_str1, '%A, %B %d, %Y')
dmy_dt2 = datetime.strptime(date_str2, '%m/%d/%y')
dmy_dt3 = datetime.strptime(date_str3, '%m-%d-%Y')

#Print the converted dates
print(dmy_dt1)
print(dmy_dt2)
print(dmy_dt3)

Output:

2017-07-14 00:00:00
2017-07-14 00:00:00
2018-07-14 00:00:00

Converting the date string column

This conversion shows how to convert whole column of date strings from the dataset to datetime format.

From now on, you have to work with the DataFrame called eth that contains the historical data on ether, and also a cryptocurrency whose blockchain is produced by the Ethereum platform. The dataset consists the following columns:

  • date: Defines the actual date, daily at 00:00 UTC.
  • txVolume: It refers an unadjusted measure of the total value in US dollars, in outputs on the blockchain.
  • txCount: It defines number of transactions performed on public blockchain.
  • marketCap: Refers to the unit price in US dollars multiplied by the number of units in circulation.
  • price: Refers an opening price in US dollars at 00:00 UTC.
  • generatedCoins: Refers the number of new coins.
  • exchangeVolume: Refers the actual volume which is measured by US dollars, at exchanges like GDAX and Bitfinex.

Next TopicPandas Plot




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