2

I'm very new to the Python world and am working on a project. The project is to read from a CSV of user data and manipulate the data to create different reports on user health.

One of these reports is to figure out how many users have logged in within the last week. The 'last login' data was strings, but I converted to int. I appended the 'last login' into a separate list.

Some users have not logged in, therefore do not have a 'last login.' I replaced the empty indexes with 0, so no indexes were empty. I'm struggling with getting the timestamp into datetime (really any format: MM/DD/YYYY, HH:MM:SS). I keep getting an error:

ValueError: year is out of range

I've tried dividing by 1000, but still having issues. Any ideas?

Below is an example of the data I'm working with in my list.

[1544626810820, 1544647898313, 1543194669126, 1533042869622, 0, 1537968162473, 1538754659570, 1539198730173, 0, 0, 1543369525149, 0, 1535642246135, 1539806567884, 0, 1511208172787, 1542840156036, 1536345371483, 1515986690566, 0, 1516799608495, 0, 1536700256748, 1543374990449, 1544555559134, 1526915269686, 0, 1534523604072, 0, 1518555543753, 1540228140771, 1538586517246, 0, 1543840813753, 1534526715930, 0, 1540654014208, 0, 1543436093259, 1544641503675, 1538160074436, 1519667756523, 1539870078998, 0, 0, 1538748889751, 1522940503466, 1530518088966, 1544717763534, 0, 1544545768879, 1539803669673, 0, 1523901643633, 1538744148771, 1537983169153, 1538591931401, 0, 0, 0, 1529003670926, 1519228170054, 0, 0, 1506366784964, 1542450066936, 1541630106764, 1543425175040, 1544390856610, 1536854673722, 1542299240573, 1543424235993, 1536878439598, 1505248601850, 0, 0, 0, 0, 0, 1544621280620, 1508260711613, 1544565233469, 1543593841774, 0, 0, 0, 0, 0, 1538591968411, 0, 0, 1505420764995, 0, 0, 1543855009700, 0, 0, 0, 0, 0, 0, 1540406592563, 0, 1543011085517, 1544544779043, 0, 0, 1510618477771, 0, 0, 1542217475312]

Code used to get the error:

intLastLogin = [int(x) for x in lastLogin]

for row in intLastLogin:
    dt = datetime.fromtimestamp(row).strftime('%Y-%m-%d %H:%M:%S')
    print dt
0

2 Answers 2

1

You can use datetime.datetime.fromtimestamp() to convert milliseconds to datetime. For example:

from datetime import datetime

timestamps = [1544626810820, 1544647898313, 1543194669126, 1533042869622, 0, 1537968162473, 1538754659570, 1539198730173, 0, 0, 1543369525149, 0, 1535642246135, 1539806567884, 0, 1511208172787, 1542840156036, 1536345371483, 1515986690566, 0, 1516799608495, 0, 1536700256748, 1543374990449, 1544555559134, 1526915269686, 0, 1534523604072, 0, 1518555543753, 1540228140771, 1538586517246, 0, 1543840813753, 1534526715930, 0, 1540654014208, 0, 1543436093259, 1544641503675, 1538160074436, 1519667756523, 1539870078998, 0, 0, 1538748889751, 1522940503466, 1530518088966, 1544717763534, 0, 1544545768879, 1539803669673, 0, 1523901643633, 1538744148771, 1537983169153, 1538591931401, 0, 0, 0, 1529003670926, 1519228170054, 0, 0, 1506366784964, 1542450066936, 1541630106764, 1543425175040, 1544390856610, 1536854673722, 1542299240573, 1543424235993, 1536878439598, 1505248601850, 0, 0, 0, 0, 0, 1544621280620, 1508260711613, 1544565233469, 1543593841774, 0, 0, 0, 0, 0, 1538591968411, 0, 0, 1505420764995, 0, 0, 1543855009700, 0, 0, 0, 0, 0, 0, 1540406592563, 0, 1543011085517, 1544544779043, 0, 0, 1510618477771, 0, 0, 1542217475312]

datetimes = [datetime.fromtimestamp(t/1000) for t in timestamps]

print datetimes[0]
# OUTPUT
# 2018-12-12 15:00:10

Also you may want to add a condition that returns something other than a datetime object for the items where you have inserted 0 as the value (otherwise, those items will return a datetime of 1970-01-01 00:00:00.

Sign up to request clarification or add additional context in comments.

1 Comment

This worked. Thank you so much for helping this newbie!!
0
from datetime import datetime
data=[1544626810820, 1544647898313, 1543194669126, 1533042869622, 0, 1537968162473, 1538754659570, 1539198730173, 0, 0, 1543369525149, 0, 1535642246135, 1539806567884, 0, 1511208172787, 1542840156036, 1536345371483, 1515986690566, 0, 1516799608495, 0, 1536700256748, 1543374990449, 1544555559134, 1526915269686, 0, 1534523604072, 0, 1518555543753, 1540228140771, 1538586517246, 0, 1543840813753, 1534526715930, 0, 1540654014208, 0, 1543436093259, 1544641503675, 1538160074436, 1519667756523, 1539870078998, 0, 0, 1538748889751, 1522940503466, 1530518088966, 1544717763534, 0, 1544545768879, 1539803669673, 0, 1523901643633, 1538744148771, 1537983169153, 1538591931401, 0, 0, 0, 1529003670926, 1519228170054, 0, 0, 1506366784964, 1542450066936, 1541630106764, 1543425175040, 1544390856610, 1536854673722, 1542299240573, 1543424235993, 1536878439598, 1505248601850, 0, 0, 0, 0, 0, 1544621280620, 1508260711613, 1544565233469, 1543593841774, 0, 0, 0, 0, 0, 1538591968411, 0, 0, 1505420764995, 0, 0, 1543855009700, 0, 0, 0, 0, 0, 0, 1540406592563, 0, 1543011085517, 1544544779043, 0, 0, 1510618477771, 0, 0, 1542217475312]
data=[datetime.utcfromtimestamp(int(x)/1000).strftime("%Y-%m-%d %H:%M:%S") for x in data]
print(data)

1 Comment

Thank you so much! Really appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.