Python long to datetime

How to Convert int64 to Datetime in Pandas?

Convert Int64 To Datetime

When dealing with datasets, they need not necessarily be in the format that we require. At times, even numerical data can be stored in textual format and instances like those might throw tantrums at the pace at which we progress, especially when gargantuan amounts of data are involved. Is there a way around this? Can anything in Python help to put us out of this misery? This article attempts to provide answers to these questions by demonstrating solutions for a specific case, one where we convert numbers into date & time format.

We shall look into the conversion of int64 data into Datetime format for each of the following categories.

To convert int64 to Datetime in Pandas, use the pd.to_datetime() function. For long dates (YYYYMMDD), use the format ‘%Y%m%d’. For short dates (YYMMDDD), use ‘%y%m%d’. For dates with time (YYMMDDHHMM), use ‘%y%m%d%H%M’. Adjust the format parameter to suit your specific date representation.

The to_datetime( ) function from the pandas library would be used for converting the int64 data into datetime. So, let us get started by importing the pandas library using the below code.

Converting Long Dates (YYYYMMDD)

This category deals with the dates that are stored in the form of continuous numbers in the int64 format. Let us have a close look at what appears when the following dataset is printed using the print function.

df = pd.DataFrame(<'When?':[20200316, 20230517, 20210525, 20221219, 20191130], 'What?': ['Convocation', 'Exams End', 'Vacation starts', 'Tour', 'Party']>) print(df) print(df.dtype)

Dataframe With Long Dates

It is evident from the above image that the data type is int64 and it is obvious that the data is not in any conventional means by which a date could be represented. So, it is time to take things in hand and do something about it using the to_datetime( ) function.

df['When?'] = pd.to_datetime(df['When?'],format = '%Y%m%d')

Run the above code and try printing the dataframe again using the print function. Also, have a look at the data types in the resulting dataframe, you might be in for a surprise.

Long Dates Converted To Datetime Format

It is such a relief to see the dates in the format they are supposed to be, isn’t it?

Converting Short Dates (YYMMDD)

Sometimes the dates in the numerical format can also be in their short forms taking only the two digits of the year rather than four. This might pose a problem when one deploys the technique detailed in the above section to convert those values into datetime format. So, here is what needs to be done, a minor tweak in the code and we are all set.

Better to witness a demonstration rather than talk it out, so have a look at the below dataset.

df = pd.DataFrame(<'When?':[200316, 230517, 210525, 221219, 191130], 'What?': ['Convocation', 'Exams End', 'Vacation starts', ‘Tour’, ‘Party’]>) print(df) print(df.dtypes)

Dataframe With Short Dates

The minor tweak here is to replace ‘%Y’ with ‘%y’ in the to_datetime function as shown below.

df['When?'] = pd.to_datetime(df['When?'],format = '%y%m%d')

Now run the code & print the results to witness the changes in the date format for all the values under the ‘When?’ column.

Short Dates Converted To Datetime Format

Converting Dates with Time (YYMMDDHHMM)

There can also be instances when the date is combined with the time and listed together. It might sound like a herculean task to sieve the dates from time, but it ain’t the case with Python. Let us have a look at how to resolve this in the following example.

df = pd.DataFrame(<'When?':[2003161915, 2305171300, 2105250700, 2212191030, 1911302130], 'What?': ['Convocation', 'Exams End', 'Vacation starts','Tour', 'Party']>) print(df) print(df.dtypes)

Dataframe With Date And Time

The last four digits in each value under the ‘When?’ column corresponds to the time in 24-hour format. Let us now separate it out from the date alongside converting the numerical value into a date using the to_datetime( ) function. Include ‘%H%M’ to isolate the time from the date.

df['When?'] = pd.to_datetime(df['When?'],format = '%y%m%d%H%M')

Run the above code and the results emerge when the print function is used to return the modified dataframe.

Converted Dates With Time

Summary

Now that we have reached the end of this article, hope it has elaborated on the different variants that can be used to convert int64 data to Datetime in Pandas. Here’s another article that details how to display numbers with leading zeros in Python. There are numerous other enjoyable and equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. Audere est facere!

Reference:

Источник

Convert Integer to datetime in Python (2 Examples)

TikTok Icon Statistics Globe

In this tutorial, you’ll learn how to convert an integer timestamp into a datetime using the Python programming language.

The tutorial includes the following sections:

Library Imports & Data Initialization

Firstly, we need to import datetime from the datetime module, pytz and the pandas library.

from datetime import datetime import pytz import pandas as pd

To avoid timezone complications, we will initialize a timezone object.

timezone = pytz.timezone('UTC')

The created UTC timezone object will be used to generate the sample datetime object and a timestamp.

sample_date = datetime(2018, 6, 15, 18, 12, 45, 15, tzinfo=timezone) print(sample_date) ts = sample_date.timestamp() print(ts) # 1529086365.000015

Example 1: Using fromtimestamp() functions

The utcfromtimestamp() function will return the datetime for the UTC timezone.

dt = datetime.utcfromtimestamp(ts) print(dt) # 2018-06-15 16:12:45.000015

The fromtimestamp() function will return the datetime for the given timezone.

dt2 = datetime.fromtimestamp(ts, tz=timezone) print(dt2) # 2018-06-15 18:12:45.000015+00:00

Note: If the timezone parameter, tz, isn’t set; then the timezone for the system will be used automatically.

Example 2: Using pandas to_datetime() function

There is also the to_datetime() function of the pandas library to convert timestamps into datetime objects. Since our timestamp’s integer part is in seconds, we will set the time unit as ‘s’.

dt3 = pd.to_datetime(ts, unit='s') print(dt3) # 2018-06-15 18:12:45.000015104

To show how the unit parameter can be utilized, we can multiply our timestamp with the value 1000 to make the integer part be in microseconds and set the unit as ‘ms’ and then get the datetime again.

dt4 = pd.to_datetime(ts*1000, unit='ms') print(dt4)

As you can see, both datetime results are the same.

Video, Further Resources & Summary

Do you need more explanations on how to convert an integer timestamp into a datetime? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

The YouTube video will be added soon.

Furthermore, you could have a look at some other tutorials on Statistics Globe:

  • Dates & Times in Python – datetime Module
  • Introduction to timedelta Objects in Python
  • Get First & Last Day of Month in Python
  • Count Working Days Between Two Dates Excluding Weekends in Python
  • Calculate Time Difference Between Two Columns of pandas DataFrame in Python
  • Calculate Number of Hours, Minutes & Seconds Between Two datetimes in Python
  • Calculate Number of Years, Months & Days Between Two Dates in Python
  • Calculate Time Difference Between Two datetime Objects in Python
  • Calculate Time Difference in Milliseconds Between Two datetimes
  • How to Add & Subtract Weeks to & from Date in Python
  • Add Days, Months & Years to datetime Object
  • Introduction to Python

This post has shown how to transform an integer timestamp into a datetime. In case you have further questions, you may leave us a comment below.

Ömer Ekiz Informatics Expert

This page was created in collaboration with Ömer Ekiz. You may have a look at Ömer’s author page to read more about his academic background and the other articles he has written for Statistics Globe.

Источник

Convert Integer to datetime in Python (2 Examples)

TikTok Icon Statistics Globe

In this tutorial, you’ll learn how to convert an integer timestamp into a datetime using the Python programming language.

The tutorial includes the following sections:

Library Imports & Data Initialization

Firstly, we need to import datetime from the datetime module, pytz and the pandas library.

from datetime import datetime import pytz import pandas as pd

To avoid timezone complications, we will initialize a timezone object.

timezone = pytz.timezone('UTC')

The created UTC timezone object will be used to generate the sample datetime object and a timestamp.

sample_date = datetime(2018, 6, 15, 18, 12, 45, 15, tzinfo=timezone) print(sample_date) ts = sample_date.timestamp() print(ts) # 1529086365.000015

Example 1: Using fromtimestamp() functions

The utcfromtimestamp() function will return the datetime for the UTC timezone.

dt = datetime.utcfromtimestamp(ts) print(dt) # 2018-06-15 16:12:45.000015

The fromtimestamp() function will return the datetime for the given timezone.

dt2 = datetime.fromtimestamp(ts, tz=timezone) print(dt2) # 2018-06-15 18:12:45.000015+00:00

Note: If the timezone parameter, tz, isn’t set; then the timezone for the system will be used automatically.

Example 2: Using pandas to_datetime() function

There is also the to_datetime() function of the pandas library to convert timestamps into datetime objects. Since our timestamp’s integer part is in seconds, we will set the time unit as ‘s’.

dt3 = pd.to_datetime(ts, unit='s') print(dt3) # 2018-06-15 18:12:45.000015104

To show how the unit parameter can be utilized, we can multiply our timestamp with the value 1000 to make the integer part be in microseconds and set the unit as ‘ms’ and then get the datetime again.

dt4 = pd.to_datetime(ts*1000, unit='ms') print(dt4)

As you can see, both datetime results are the same.

Video, Further Resources & Summary

Do you need more explanations on how to convert an integer timestamp into a datetime? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

The YouTube video will be added soon.

Furthermore, you could have a look at some other tutorials on Statistics Globe:

  • Dates & Times in Python – datetime Module
  • Introduction to timedelta Objects in Python
  • Get First & Last Day of Month in Python
  • Count Working Days Between Two Dates Excluding Weekends in Python
  • Calculate Time Difference Between Two Columns of pandas DataFrame in Python
  • Calculate Number of Hours, Minutes & Seconds Between Two datetimes in Python
  • Calculate Number of Years, Months & Days Between Two Dates in Python
  • Calculate Time Difference Between Two datetime Objects in Python
  • Calculate Time Difference in Milliseconds Between Two datetimes
  • How to Add & Subtract Weeks to & from Date in Python
  • Add Days, Months & Years to datetime Object
  • Introduction to Python

This post has shown how to transform an integer timestamp into a datetime. In case you have further questions, you may leave us a comment below.

Ömer Ekiz Informatics Expert

This page was created in collaboration with Ömer Ekiz. You may have a look at Ömer’s author page to read more about his academic background and the other articles he has written for Statistics Globe.

Источник

Читайте также:  Def что значит питон
Оцените статью