w3resource

Pandas: Converting integer or float epoch times to Timestamp and DatetimeIndex


Write a Pandas program to convert integer or float epoch times to Timestamp and DatetimeIndex.

Sample Solution:

Python Code :

import pandas as pd
dates1 = pd.to_datetime([1329806505, 129806505, 1249892905,
                1249979305, 1250065705], unit='s')
print("Convert integer or float epoch times to Timestamp and DatetimeIndex upto second:")
print(dates1)
print("\nConvert integer or float epoch times to Timestamp and DatetimeIndex upto milisecond:")
dates2 = pd.to_datetime([1249720105100, 1249720105200, 1249720105300,
                1249720105400, 1249720105500], unit='ms')
print(dates2)

Sample Output:

Convert integer or float epoch times to Timestamp and DatetimeIndex upto second:
DatetimeIndex(['2012-02-21 06:41:45', '1974-02-11 09:21:45',
               '2009-08-10 08:28:25', '2009-08-11 08:28:25',
               '2009-08-12 08:28:25'],
              dtype='datetime64[ns]', freq=None)

Convert integer or float epoch times to Timestamp and DatetimeIndex upto milisecond:
DatetimeIndex(['2009-08-08 08:28:25.100000', '2009-08-08 08:28:25.200000',
               '2009-08-08 08:28:25.300000', '2009-08-08 08:28:25.400000',
               '2009-08-08 08:28:25.500000'],
              dtype='datetime64[ns]', freq=None)

Python Code Editor:

//------------- this portion above included on 07-02-2025 ------------------------------ var new_txt = 'Based on '+total_submit+' votes, average difficulty level of this exercise is '+difficulty+'.'; //'.  '+difficulty+'/3'; var txt_node = document.createTextNode(new_txt); var level_result = document.getElementById('level_result'); level_result.appendChild(txt_node); } } else { alert('There was a problem with the request.'); } } } } function insert_level(event) { event.preventDefault(); var path = window.location; var page = path.href; var page = page.split('?'); var page = page[0]; //console.log(page); //console.log(page); /*var btns = document.getElementsByClassName("mdl-button mdl-js-button mdl-button--raised mdl-button--colored"); for (var i = 0; i < btns.length; i++) { var clicked = btns[i].id; }*/ var clicked = this.id; if(clicked=="easy") clicked=1; if(clicked=="medium") clicked=2; if(clicked=="hard") clicked=3; console.log(clicked); var httpRequest1 = new XMLHttpRequest(); if (!httpRequest1) { alert('Giving up :( Cannot create an XMLHTTP instance'); //return false; } var url = "/assets/level_insert.php"; var data1 = "level=" + clicked + "&page=" + page; httpRequest1.onreadystatechange = displayContent1; httpRequest1.open("POST", url, true); httpRequest1.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); httpRequest1.send(data1); console.log(data1); //console.log("found"); function displayContent1(responseText) { if (httpRequest1.readyState === XMLHttpRequest.DONE) { if (httpRequest1.status === 200) { var op = httpRequest1.responseText; console.log(op); } else { alert('There was a problem with the request.'); } } } } var easy = document.getElementById("easy"); easy.addEventListener('click', insert_level, false); var medium = document.getElementById("medium"); medium.addEventListener('click', insert_level, false); var hard = document.getElementById("hard"); hard.addEventListener('click', insert_level, false);

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.