Convert number to date python
Python and Matlab quite often have integer date representations as follows: Show
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
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:
Also, note that while looking similar these are actually different things in Matlab and Python:
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:
PSI 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 datetimeTo 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:
Step 2: Create the DataFrameNext, 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:
Step 3: Convert the integers to datetime in Pandas DataFrameNow 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:
Converting Additional FormatsLet’s suppose that the dates are now formatted as yymmdd:
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:
Now let’s suppose that your integers contain both the dates and times:
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:
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)). |