{
  "id": 66125,
  "title": "visitStartTime",
  "url": "/competitions/ga-customer-revenue-prediction/discussion/66125",
  "author_name": "Joash Colaco",
  "post_date": "2018-09-18T12:26:02.850000",
  "votes": 4,
  "comment_count": 14,
  "views": 0,
  "content": "<p>Can someone please help me format the visitStartTime field. How would you transform 1499484877 into a time?</p>",
  "messages": [
    {
      "id": 389264,
      "postDate": "2018-09-18T12:57:02.930Z",
      "content": "<p>This is how to do in python.  I'm  not sure how in R.</p>\n\n<p>import datetime as dt</p>\n\n<p>df['time'] = df.visitStartTime.apply(lambda x: dt.datetime.utcfromtimestamp(float(x)))</p>",
      "rawMarkdown": "This is how to do in python.  I'm  not sure how in R.\n\nimport datetime as dt\n\ndf['time'] = df.visitStartTime.apply(lambda x: dt.datetime.utcfromtimestamp(float(x)))",
      "votes": 7
    },
    {
      "id": 389253,
      "postDate": "2018-09-18T12:26:02.850Z",
      "content": "<p>Can someone please help me format the visitStartTime field. How would you transform 1499484877 into a time?</p>",
      "rawMarkdown": "Can someone please help me format the visitStartTime field. How would you transform 1499484877 into a time?",
      "votes": 4
    },
    {
      "id": 391279,
      "postDate": "2018-09-21T14:16:40.807Z",
      "content": "<p>I am simply using as.POSIXct(visitStartTime, origin=\"1970-01-01\", tz=\"UTC\"), and once you get the hour, minute and seconds extracted, I also find it useful to have a \"continuous\" time feature each day by doing something like (hour + minute/60 + seconds/3600).</p>",
      "rawMarkdown": "I am simply using as.POSIXct(visitStartTime, origin=\"1970-01-01\", tz=\"UTC\"), and once you get the hour, minute and seconds extracted, I also find it useful to have a \"continuous\" time feature each day by doing something like (hour + minute/60 + seconds/3600).",
      "votes": 2,
      "replies": [
        {
          "id": 391307,
          "postDate": "2018-09-21T15:20:18Z",
          "content": "<p>Hey yilisg,</p>\n\n<p>I'm not too sure what you mean but I take that you compute some ratio that tells you how far in the way we are at? I did the same thing with the following piece of code.</p>\n\n<p><code>python\ntime = df['visitStartTime']\nprogress = (time.dt.hour * (60 * 60) + time.dt.minutes * 60 + time.dt.seconds) / (24 * 60 * 60)\n</code></p>\n\n<p>This returns a value between 0 and 1.</p>",
          "rawMarkdown": "Hey yilisg,\n\nI'm not too sure what you mean but I take that you compute some ratio that tells you how far in the way we are at? I did the same thing with the following piece of code.\n\n```python\ntime = df['visitStartTime']\nprogress = (time.dt.hour * (60 * 60) + time.dt.minutes * 60 + time.dt.seconds) / (24 * 60 * 60)\n```\n\nThis returns a value between 0 and 1."
        },
        {
          "id": 391314,
          "postDate": "2018-09-21T15:30:44.593Z",
          "content": "<p>I guess that works too. For me it's just a number between 0 and 24. Sry about the typo above.</p>",
          "rawMarkdown": "I guess that works too. For me it's just a number between 0 and 24. Sry about the typo above."
        },
        {
          "id": 391352,
          "postDate": "2018-09-21T17:06:20.743Z",
          "content": "<p>No worries! I just wanted to make things clear :)</p>",
          "rawMarkdown": "No worries! I just wanted to make things clear :)"
        }
      ]
    },
    {
      "id": 391099,
      "postDate": "2018-09-21T09:11:38.317Z",
      "content": "<p>Even quicker: <code>df['visitStartTime'] = pd.to_datetime(df['visitStartTime'], unit='s')</code></p>",
      "rawMarkdown": "Even quicker: `df['visitStartTime'] = pd.to_datetime(df['visitStartTime'], unit='s')`",
      "votes": 2,
      "replies": [
        {
          "id": 391101,
          "postDate": "2018-09-21T09:14:55.650Z",
          "content": "<p>There is a problem with <code>pd.to_datetime</code>. It does not interpret the timestamp correctly.\nFor this reason its better to use the external <code>datetime</code> module.</p>",
          "rawMarkdown": "There is a problem with ` pd.to_datetime`. It does not interpret the timestamp correctly.\nFor this reason its better to use the external `datetime` module.",
          "isDeleted": true
        },
        {
          "id": 391103,
          "postDate": "2018-09-21T09:16:05.790Z",
          "content": "<p>Can you be more specific?</p>",
          "rawMarkdown": "Can you be more specific?"
        },
        {
          "id": 391110,
          "postDate": "2018-09-21T09:26:52.197Z",
          "content": "<p>Sorry for the confusion. You are totally right your solution is pretty neat.\nI tried the same without specifying <code>unit=\"s\"</code> and got the wrong dates.</p>",
          "rawMarkdown": "Sorry for the confusion. You are totally right your solution is pretty neat.\nI tried the same without specifying `unit=\"s\"` and got the wrong dates.",
          "votes": 1,
          "isDeleted": true
        },
        {
          "id": 391111,
          "postDate": "2018-09-21T09:28:24.100Z",
          "content": "<p>Cool! You nearly got me to doubt ;)</p>",
          "rawMarkdown": "Cool! You nearly got me to doubt ;)"
        }
      ]
    },
    {
      "id": 391081,
      "postDate": "2018-09-21T08:43:00.897Z",
      "content": "<p>For R, I would use <code>lubridate</code> library.</p>\n\n<p>The result of the code below matches with that of <a href=\"https://www.epochconverter.com/\">Epoch Converter</a> :)</p>\n\n<pre>library(lubridate)\nlibrary(tidyverse)\ndf %&gt;% mutate(visitTime = as_datetime(visitStartTime))\n</pre>",
      "rawMarkdown": "For R, I would use `lubridate` library.\n\nThe result of the code below matches with that of [Epoch Converter][1] :)\n\n<pre>library(lubridate)\nlibrary(tidyverse)\ndf %&gt;% mutate(visitTime = as_datetime(visitStartTime))\n</pre>\n\n  [1]: https://www.epochconverter.com/"
    },
    {
      "id": 389734,
      "postDate": "2018-09-19T06:27:47.967Z",
      "content": "<p>Hi Joash, You can use <strong>anytime</strong> package ( <a href=\"https://stackoverflow.com/questions/13456241/convert-unix-epoch-to-date-object\">Here</a>) in R to extract Unix/Epoch timestamp to datetime format but \nR conversion does not seem to match with Micheal's command in python. I checked it with <a href=\"https://www.epochconverter.com/\">Epoch Converter</a>, the results from this link matches with Micheal's command. So it is safer to use the python command.</p>",
      "rawMarkdown": "Hi Joash, You can use **anytime** package ( [Here][1]) in R to extract Unix/Epoch timestamp to datetime format but \nR conversion does not seem to match with Micheal's command in python. I checked it with [Epoch Converter][2], the results from this link matches with Micheal's command. So it is safer to use the python command.\n\n\n  [1]: https://stackoverflow.com/questions/13456241/convert-unix-epoch-to-date-object\n  [2]: https://www.epochconverter.com/"
    },
    {
      "id": 390668,
      "postDate": "2018-09-20T15:59:02.370Z",
      "content": "<p>I recommend you run this line right after.</p>\n\n<pre><code>df.time =  pd.to_datetime(df.time)\n</code></pre>\n\n<p>This way you make sure pandas converts the <code>time</code> column to <code>dtype: datetime64[ns]</code>, which opens up some handy featrures.</p>",
      "rawMarkdown": "I recommend you run this line right after.\n\n    df.time =  pd.to_datetime(df.time)\n\nThis way you make sure pandas converts the `time` column to `dtype: datetime64[ns]`, which opens up some handy featrures.",
      "isDeleted": true
    },
    {
      "id": 389403,
      "postDate": "2018-09-18T16:56:52.347Z",
      "content": "<p>Thank you very much!</p>",
      "rawMarkdown": "Thank you very much!"
    }
  ],
  "comments": [
    {
      "id": 389264,
      "author_name": "Michael Maguire",
      "author_url": "",
      "post_date": "2018-09-18T12:57:02.930000",
      "content": "<p>This is how to do in python.  I'm  not sure how in R.</p>\n\n<p>import datetime as dt</p>\n\n<p>df['time'] = df.visitStartTime.apply(lambda x: dt.datetime.utcfromtimestamp(float(x)))</p>",
      "votes": 7,
      "replies": []
    },
    {
      "id": 391279,
      "author_name": "yilisg",
      "author_url": "",
      "post_date": "2018-09-21T14:16:40.807000",
      "content": "<p>I am simply using as.POSIXct(visitStartTime, origin=\"1970-01-01\", tz=\"UTC\"), and once you get the hour, minute and seconds extracted, I also find it useful to have a \"continuous\" time feature each day by doing something like (hour + minute/60 + seconds/3600).</p>",
      "votes": 2,
      "replies": [
        {
          "id": 391307,
          "author_name": "Max Halford",
          "author_url": "",
          "post_date": "2018-09-21T15:20:18",
          "content": "<p>Hey yilisg,</p>\n\n<p>I'm not too sure what you mean but I take that you compute some ratio that tells you how far in the way we are at? I did the same thing with the following piece of code.</p>\n\n<p><code>python\ntime = df['visitStartTime']\nprogress = (time.dt.hour * (60 * 60) + time.dt.minutes * 60 + time.dt.seconds) / (24 * 60 * 60)\n</code></p>\n\n<p>This returns a value between 0 and 1.</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 391314,
          "author_name": "yilisg",
          "author_url": "",
          "post_date": "2018-09-21T15:30:44.593000",
          "content": "<p>I guess that works too. For me it's just a number between 0 and 24. Sry about the typo above.</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 391352,
          "author_name": "Max Halford",
          "author_url": "",
          "post_date": "2018-09-21T17:06:20.743000",
          "content": "<p>No worries! I just wanted to make things clear :)</p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 391099,
      "author_name": "Max Halford",
      "author_url": "",
      "post_date": "2018-09-21T09:11:38.317000",
      "content": "<p>Even quicker: <code>df['visitStartTime'] = pd.to_datetime(df['visitStartTime'], unit='s')</code></p>",
      "votes": 2,
      "replies": [
        {
          "id": 391101,
          "author_name": "",
          "author_url": "",
          "post_date": "2018-09-21T09:14:55.650000",
          "content": "<p>There is a problem with <code>pd.to_datetime</code>. It does not interpret the timestamp correctly.\nFor this reason its better to use the external <code>datetime</code> module.</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 391103,
          "author_name": "Max Halford",
          "author_url": "",
          "post_date": "2018-09-21T09:16:05.790000",
          "content": "<p>Can you be more specific?</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 391110,
          "author_name": "",
          "author_url": "",
          "post_date": "2018-09-21T09:26:52.197000",
          "content": "<p>Sorry for the confusion. You are totally right your solution is pretty neat.\nI tried the same without specifying <code>unit=\"s\"</code> and got the wrong dates.</p>",
          "votes": 1,
          "replies": []
        },
        {
          "id": 391111,
          "author_name": "Max Halford",
          "author_url": "",
          "post_date": "2018-09-21T09:28:24.100000",
          "content": "<p>Cool! You nearly got me to doubt ;)</p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 391081,
      "author_name": "honake",
      "author_url": "",
      "post_date": "2018-09-21T08:43:00.897000",
      "content": "<p>For R, I would use <code>lubridate</code> library.</p>\n\n<p>The result of the code below matches with that of <a href=\"https://www.epochconverter.com/\">Epoch Converter</a> :)</p>\n\n<pre>library(lubridate)\nlibrary(tidyverse)\ndf %&gt;% mutate(visitTime = as_datetime(visitStartTime))\n</pre>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 389734,
      "author_name": "Prashanth Thangavel",
      "author_url": "",
      "post_date": "2018-09-19T06:27:47.967000",
      "content": "<p>Hi Joash, You can use <strong>anytime</strong> package ( <a href=\"https://stackoverflow.com/questions/13456241/convert-unix-epoch-to-date-object\">Here</a>) in R to extract Unix/Epoch timestamp to datetime format but \nR conversion does not seem to match with Micheal's command in python. I checked it with <a href=\"https://www.epochconverter.com/\">Epoch Converter</a>, the results from this link matches with Micheal's command. So it is safer to use the python command.</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 390668,
      "author_name": "",
      "author_url": "",
      "post_date": "2018-09-20T15:59:02.370000",
      "content": "<p>I recommend you run this line right after.</p>\n\n<pre><code>df.time =  pd.to_datetime(df.time)\n</code></pre>\n\n<p>This way you make sure pandas converts the <code>time</code> column to <code>dtype: datetime64[ns]</code>, which opens up some handy featrures.</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 389403,
      "author_name": "Joash Colaco",
      "author_url": "",
      "post_date": "2018-09-18T16:56:52.347000",
      "content": "<p>Thank you very much!</p>",
      "votes": 0,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "389264": "This is how to do in python.  I'm  not sure how in R.\n\nimport datetime as dt\n\ndf['time'] = df.visitStartTime.apply(lambda x: dt.datetime.utcfromtimestamp(float(x)))",
    "389253": "Can someone please help me format the visitStartTime field. How would you transform 1499484877 into a time?",
    "391279": "I am simply using as.POSIXct(visitStartTime, origin=\"1970-01-01\", tz=\"UTC\"), and once you get the hour, minute and seconds extracted, I also find it useful to have a \"continuous\" time feature each day by doing something like (hour + minute/60 + seconds/3600).",
    "391099": "Even quicker: `df['visitStartTime'] = pd.to_datetime(df['visitStartTime'], unit='s')`",
    "391081": "For R, I would use `lubridate` library.\n\nThe result of the code below matches with that of [Epoch Converter][1] :)\n\n<pre>library(lubridate)\nlibrary(tidyverse)\ndf %&gt;% mutate(visitTime = as_datetime(visitStartTime))\n</pre>\n\n  [1]: https://www.epochconverter.com/",
    "389734": "Hi Joash, You can use **anytime** package ( [Here][1]) in R to extract Unix/Epoch timestamp to datetime format but \nR conversion does not seem to match with Micheal's command in python. I checked it with [Epoch Converter][2], the results from this link matches with Micheal's command. So it is safer to use the python command.\n\n\n  [1]: https://stackoverflow.com/questions/13456241/convert-unix-epoch-to-date-object\n  [2]: https://www.epochconverter.com/",
    "390668": "I recommend you run this line right after.\n\n    df.time =  pd.to_datetime(df.time)\n\nThis way you make sure pandas converts the `time` column to `dtype: datetime64[ns]`, which opens up some handy featrures.",
    "389403": "Thank you very much!"
  }
}