Convert number to date python

Python and Matlab quite often have integer date representations as follows:

733828.0 733829.0 733832.0 733833.0 733834.0 733835.0 733836.0 733839.0 733840.0 733841.0

these numbers correspond to some dates this year. Do you guys know which function can convert them back to YYYYMMDD format?

thanks a million!

John Topley

112k46 gold badges193 silver badges237 bronze badges

asked Apr 12, 2010 at 15:21

0

The datetime.datetime class can help you here. The following works, if those values are treated as integer days [you don't specify what they are].

>>> from datetime import datetime
>>> dt = datetime.fromordinal[733828]
>>> dt
datetime.datetime[2010, 2, 25, 0, 0]
>>> dt.strftime['%Y%m%d']
'20100225'

You show the values as floats, and the above doesn't take floats. If you can give more detail about what the data is [and where it comes from] it will be possible to give a more complete answer.

answered Apr 12, 2010 at 15:26

Peter HansenPeter Hansen

20.5k4 gold badges47 silver badges72 bronze badges

3

Since Python example was already demonstrated, here is the matlab one:

>> datestr[733828, 'yyyymmdd']

ans =

20090224

Also, note that while looking similar these are actually different things in Matlab and Python:

Matlab
A serial date number represents the whole and fractional number of days from a specific date and time, where datenum['Jan-1-0000 00:00:00'] returns the number 1. [The year 0000 is merely a reference point and is not intended to be interpreted as a real year in time.]

Python, datetime.date.fromordinal
Return the date corresponding to the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1.

So they would differ by 366 days, which is apparently the length of the year 0.

answered Apr 12, 2010 at 15:27

SilentGhostSilentGhost

294k64 gold badges301 silver badges291 bronze badges

5

Dates like 733828.0 are Rata Die dates, counted from January 1, 1 A.D. [and decimal fraction of days]. They may be UTC or by your timezone.

Julian Dates, used mostly by astronomers, count the days [and decimal fraction of days] since January 1, 4713 BC Greenwich noon. Julian date is frequently confused with Ordinal date, which is the date count from January 1 of the current year [Feb 2 = ordinal day 33].

So datetime is calling these things ordinal dates, but I think this only makes sense locally, in the world of python.

answered Jun 21, 2012 at 17:46

1

Is 733828.0 a timestamp? If so, you can do the following:

import datetime as dt
dt.date.fromtimestamp[733828.0].strftime['%Y%m%d']

PS

I think Peter Hansen is right :]

I am not a native English speaker. Just trying to help. I don't quite know the difference between a timestamp and an ordinal :[

answered Apr 12, 2010 at 15:25

satorusatoru

30.3k29 gold badges87 silver badges138 bronze badges

1

Here is the syntax that you may use to convert integers to datetime in Pandas DataFrame:

df['DataFrame Column'] = pd.to_datetime[df['DataFrame Column'], format=specify your format]

Note that the integers must match the format specified. Later, you’ll see several scenarios for different formats.

Step 1: Gather the data to be converted to datetime

To start, gather the data that you’d like to convert to datetime.

For example, the following dataset contains 3 different dates [with a format of yyyymmdd], when a store might be opened or closed:

Dates Status
20210305 Opened
20210316 Opened
20210328 Closed

Step 2: Create the DataFrame

Next, create the DataFrame to capture the above dataset in Python:

import pandas as pd

values = {'dates':  [20210305,20210316,20210328],
          'status': ['Opened','Opened','Closed']
          }

df = pd.DataFrame[values, columns = ['dates','status']]

print [df]
print [df.dtypes]

As you may see, the data type is integer for the values under the ‘dates’ column:

      dates  status
0  20210305  Opened
1  20210316  Opened
2  20210328  Closed
dates      int64
status    object
dtype: object

Step 3: Convert the integers to datetime in Pandas DataFrame

Now you may use the template below in order to convert the integers to datetime in Pandas DataFrame:

df['DataFrame Column'] = pd.to_datetime[df['DataFrame Column'], format=specify your format]

Recall that for our example, the date format is yyyymmdd.

In that case, the date format can be represented as follows:

format='%Y%m%d'

As indicated previously, the integer data [yyyymmdd] must match the format specified [%Y%m%d]. You may refer to the following source for the different formats that you may apply.

For our example, the complete code to convert the integers to datetime would be:

import pandas as pd

values = {'dates':  [20210305,20210316,20210328],
          'status': ['Opened','Opened','Closed']
          }

df = pd.DataFrame[values, columns = ['dates','status']]

df['dates'] = pd.to_datetime[df['dates'], format='%Y%m%d']

print [df]
print [df.dtypes]

Run the code in Python, and you’ll see that the data type for the ‘dates’ is now datetime:

       dates  status
0 2021-03-05  Opened
1 2021-03-16  Opened
2 2021-03-28  Closed
dates     datetime64[ns]
status            object
dtype: object

Converting Additional Formats

Let’s suppose that the dates are now formatted as yymmdd:

Dates Status
210305 Opened
210316 Opened
210328 Closed

In that case, the date format would now contain ‘y‘ in lower case:

format='%y%m%d'

So the complete Python code would look as follows:

import pandas as pd

values = {'dates':  [210305,210316,210328],
          'status': ['Opened','Opened','Closed']
          }

df = pd.DataFrame[values, columns = ['dates','status']]

df['dates'] = pd.to_datetime[df['dates'], format='%y%m%d']

print [df]
print [df.dtypes]

As before, the integers would get converted to datetime:

       dates  status
0 2021-03-05  Opened
1 2021-03-16  Opened
2 2021-03-28  Closed
dates     datetime64[ns]
status            object
dtype: object

Now let’s suppose that your integers contain both the dates and times:

Dates Status
20210305093000 Opened
20210316093000 Opened
20210328200000 Closed

In that case, the format that you should specify is:

format='%Y%m%d%H%M%S'

So the full Python code would be:

import pandas as pd

values = {'dates':  [20210305093000,20210316093000,20210328200000],
          'status': ['Opened','Opened','Closed']
          }

df = pd.DataFrame[values, columns = ['dates','status']]

df['dates'] = pd.to_datetime[df['dates'], format='%Y%m%d%H%M%S']

print [df]
print [df.dtypes]

You’ll now get the datetime format:

                dates  status
0 2021-03-05 09:30:00  Opened
1 2021-03-16 09:30:00  Opened
2 2021-03-28 20:00:00  Closed
dates     datetime64[ns]
status            object
dtype: object

You may also want to check the following guide for the steps to convert strings to datetime in Pandas DataFrame.

How do you convert a number to a date in Python?

Practical Data Science using Python You can use the fromtimestamp function from the datetime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the datetime object corresponding to the timestamp.

How do I convert a number to a date in a DataFrame in Python?

Steps to Convert Integers to Datetime in Pandas DataFrame.
Step 1: Gather the data to be converted to datetime. To start, gather the data that you'd like to convert to datetime. ... .
Step 2: Create the DataFrame. ... .
Step 3: Convert the integers to datetime in Pandas DataFrame..

How do you convert a 5 digit number to a date in Python?

In other words you want datetime. utcfromtimestamp[n * 86400]. strftime["%Y-%m-%d"] [with the input in n , and after from datetime import datetime ]. There may be some symbolic constant in place of the 86400 [number of seconds in a day], but it's scarcely about to change if you hard-code it!

How do you convert input to date in Python?

from datetime import datetime..
date_time_str = '18/09/19 01:55:19'.
date_time_obj = datetime. strptime[date_time_str, '%d/%m/%y %H:%M:%S'].
print ["The type of the date is now", type[date_time_obj]].

Chủ Đề