{
  "id": 438165,
  "title": "[advice?] timestamp column does not convert",
  "url": "/competitions/child-mind-institute-detect-sleep-states/discussion/438165",
  "author_name": "Andrada",
  "post_date": "2023-09-09T19:27:47.770000",
  "votes": 10,
  "comment_count": 17,
  "views": 0,
  "content": "<p>Hi all!</p>\n<p>I have a weird problem and I do not understand what am I missing.</p>\n<p>Within the <code>train_series</code> dataset I would like to convert the <code>timestamp</code> variable from dtype object to dtype datetime.</p>\n<p>If I test my function on a small piece of data, it returns something like this:<br>\n<img src=\"https://i.imgur.com/4pZ5Sv4.png\"></p>\n<p>All looks good. But when I try to apply the same code on the entire 140mil rows, the <code>timestamp</code> column does not convert. Moreover, it shows that the allocated memory for this column is bigger.</p>\n<p>I tried randomly sampling the data and take 10k rows and it's the same story, the column does not convert.<br>\n<img src=\"https://i.imgur.com/bXzjmLi.png\"></p>\n<p>Can anybody explain me why is this happening? And maybe if there is a fix to this?</p>\n<p>Thank you and happy day ^^</p>",
  "messages": [
    {
      "id": 2431100,
      "postDate": "2023-09-09T19:27:47.770Z",
      "content": "<p>Hi all!</p>\n<p>I have a weird problem and I do not understand what am I missing.</p>\n<p>Within the <code>train_series</code> dataset I would like to convert the <code>timestamp</code> variable from dtype object to dtype datetime.</p>\n<p>If I test my function on a small piece of data, it returns something like this:<br>\n<img src=\"https://i.imgur.com/4pZ5Sv4.png\"></p>\n<p>All looks good. But when I try to apply the same code on the entire 140mil rows, the <code>timestamp</code> column does not convert. Moreover, it shows that the allocated memory for this column is bigger.</p>\n<p>I tried randomly sampling the data and take 10k rows and it's the same story, the column does not convert.<br>\n<img src=\"https://i.imgur.com/bXzjmLi.png\"></p>\n<p>Can anybody explain me why is this happening? And maybe if there is a fix to this?</p>\n<p>Thank you and happy day ^^</p>",
      "rawMarkdown": "Hi all!\n\nI have a weird problem and I do not understand what am I missing.\n\nWithin the `train_series` dataset I would like to convert the `timestamp` variable from dtype object to dtype datetime.\n\nIf I test my function on a small piece of data, it returns something like this:\n<img src=\"https://i.imgur.com/4pZ5Sv4.png\", width=500>\n\nAll looks good. But when I try to apply the same code on the entire 140mil rows, the `timestamp` column does not convert. Moreover, it shows that the allocated memory for this column is bigger.\n\nI tried randomly sampling the data and take 10k rows and it's the same story, the column does not convert.\n<img src=\"https://i.imgur.com/bXzjmLi.png\", width=500>\n\nCan anybody explain me why is this happening? And maybe if there is a fix to this?\n\nThank you and happy day ^^",
      "votes": 10
    },
    {
      "id": 2431106,
      "postDate": "2023-09-09T19:33:18.903Z",
      "content": "<p>Dear <a href=\"https://www.kaggle.com/andradaolteanu\" target=\"_blank\">@andradaolteanu</a> </p>\n<p>Perhaps try </p>\n<pre><code>df[] = pd.to_datetime(df[],utc=)\n</code></pre>\n<p>All the best,<br>\ncarl</p>",
      "rawMarkdown": "Dear @andradaolteanu \n\nPerhaps try \n\n```python\ndf[\"timestamp\"] = pd.to_datetime(df[\"timestamp\"],utc=True)\n```\n\nAll the best,\ncarl",
      "votes": 8,
      "replies": [
        {
          "id": 2431755,
          "postDate": "2023-09-10T11:29:13.610Z",
          "content": "<p>YES IT WORKED! Thank you! ❤️</p>",
          "rawMarkdown": "YES IT WORKED! Thank you! ❤️",
          "votes": 2
        },
        {
          "id": 2432927,
          "postDate": "2023-09-11T08:03:40.287Z",
          "content": "<p><a href=\"https://www.kaggle.com/carlmcbrideellis\" target=\"_blank\">@carlmcbrideellis</a> - your suggestion works, but it will give UTC, which may give an inconsistent picture across different geographical locations.</p>\n<p>To get local time one can do:</p>\n<pre><code>df[] = pd.to_datetime(df[]).apply( t: t.tz_localize())\n</code></pre>\n<p>Note that one needs to <code>apply</code> localisation, because that function cannot vectorise across different timezones.</p>",
          "rawMarkdown": "@carlmcbrideellis - your suggestion works, but it will give UTC, which may give an inconsistent picture across different geographical locations.\n\nTo get local time one can do:\n\n```python\ndf['timestamp'] = pd.to_datetime(df['timestamp']).apply(lambda t: t.tz_localize(None))\n```\n\nNote that one needs to `apply` localisation, because that function cannot vectorise across different timezones.",
          "votes": 7,
          "replies": [
            {
              "id": 2433594,
              "postDate": "2023-09-11T17:10:40.577Z",
              "content": "<p>Dear <a href=\"https://www.kaggle.com/jagofc\" target=\"_blank\">@jagofc</a> </p>\n<p>Good point! <br>\n(Shame that using an <code>apply</code> is sooo slowwww!)</p>\n<p>All the best,<br>\ncarl</p>",
              "rawMarkdown": "Dear @jagofc \n\nGood point! \n(Shame that using an `apply` is sooo slowwww!)\n\nAll the best,\ncarl",
              "votes": 1
            },
            {
              "id": 2433844,
              "postDate": "2023-09-11T22:08:29.913Z",
              "rawMarkdown": "",
              "votes": 4,
              "isDeleted": true
            },
            {
              "id": 2434272,
              "postDate": "2023-09-12T07:42:17.347Z",
              "content": "<p><a href=\"https://www.kaggle.com/weka511\" target=\"_blank\">@weka511</a> - it's true. Just my take - I'm using <code>step</code> as the unique indexer, and local time just as a source of features. Also nothing wrong with preserving <code>timestamp</code> (utc as per <a href=\"https://www.kaggle.com/carlmcbrideellis\" target=\"_blank\">@carlmcbrideellis</a>) as an indexer and defining a new column <code>timestamp_local</code> for local time features instead of overwriting <code>timestamp</code>.</p>",
              "rawMarkdown": "@weka511 - it's true. Just my take - I'm using `step` as the unique indexer, and local time just as a source of features. Also nothing wrong with preserving `timestamp` (utc as per @carlmcbrideellis) as an indexer and defining a new column `timestamp_local` for local time features instead of overwriting `timestamp`.",
              "votes": 2
            },
            {
              "id": 2434380,
              "postDate": "2023-09-12T09:06:12.340Z",
              "rawMarkdown": "",
              "votes": 3,
              "isDeleted": true
            },
            {
              "id": 2434438,
              "postDate": "2023-09-12T09:47:23.117Z",
              "content": "<p>Dear <a href=\"https://www.kaggle.com/jagofc\" target=\"_blank\">@jagofc</a>  and <a href=\"https://www.kaggle.com/weka511\" target=\"_blank\">@weka511</a> </p>\n<p>Solved: </p>\n<pre><code>df[] = pd.to_datetime(df[]).apply( t: t.tz_localize())\n</code></pre>\n<p>is indeed the way too go; see my trivial test here: <a href=\"https://www.kaggle.com/code/carlmcbrideellis/zzzs-eda-utc-or-no-utc\" target=\"_blank\">\"Zzzs: EDA: UTC or no UTC\"</a></p>\n<p>Using UTC:</p>\n<p>Wake in the morning:         3925<br>\nGo to sleep in the morning:  4693<br>\nSleep in the afternoon:      100<br>\nWake in the afternoon:       869</p>\n<p>Using local</p>\n<p>Wake in the morning:         4760<br>\nGo to sleep in the morning:  956<br>\nSleep in the afternoon:      3837<br>\nWake in the afternoon:       34</p>\n<p>All the best!<br>\ncarl</p>",
              "rawMarkdown": "Dear @jagofc  and @weka511 \n\nSolved: \n\n```python\ndf['timestamp'] = pd.to_datetime(df['timestamp']).apply(lambda t: t.tz_localize(None))\n```\n\nis indeed the way too go; see my trivial test here: [\"Zzzs: EDA: UTC or no UTC\"](https://www.kaggle.com/code/carlmcbrideellis/zzzs-eda-utc-or-no-utc)\n\nUsing UTC:\n\nWake in the morning:         3925\nGo to sleep in the morning:  4693\nSleep in the afternoon:      100\nWake in the afternoon:       869\n\nUsing local\n\nWake in the morning:         4760\nGo to sleep in the morning:  956\nSleep in the afternoon:      3837\nWake in the afternoon:       34\n\n\nAll the best!\ncarl",
              "votes": 3
            }
          ]
        }
      ]
    },
    {
      "id": 2431139,
      "postDate": "2023-09-09T20:32:04.227Z",
      "content": "<p>There is a timezone at the end of the timestamp in string format, that doesn't seem to be recognized by pd.todatetime.<br>\nOne work around seems to be to cut the string to remove the timezone. (maybe it should be kept elsewhere for modelling).</p>\n<pre><code> datetime\ntargets = pd.read_csv()\ntargets.timestamp = targets.timestamp.[:-]\ntargets.timestamp = pd.to_datetime(targets.timestamp)\n</code></pre>",
      "rawMarkdown": "There is a timezone at the end of the timestamp in string format, that doesn't seem to be recognized by pd.todatetime.\nOne work around seems to be to cut the string to remove the timezone. (maybe it should be kept elsewhere for modelling).\n\n```python\nimport datetime\ntargets = pd.read_csv('/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv')\ntargets.timestamp = targets.timestamp.str[:-8]\ntargets.timestamp = pd.to_datetime(targets.timestamp)\n```",
      "votes": 6,
      "replies": [
        {
          "id": 2433428,
          "postDate": "2023-09-11T15:28:28.623Z",
          "content": "<p>I second this.  Since this has to deal with sleep cycles, the timezone (while might be significant for regional grouping in case a timezone has different sleeping habits; which might be novel to investigate), it seems like the hour relative to daylight is a more important feature than normalizing the times.</p>\n<p>Think  of it this way, for those with a sleep cycle based on day-night, it's more important to know the relative hour in terms of daylight (for example 8pm when truncating the timezone should have relatively similar amounts of daylight, so it's more of an apples-to-apples comparison, whereas a UTC conversion loses this detail).</p>",
          "rawMarkdown": "I second this.  Since this has to deal with sleep cycles, the timezone (while might be significant for regional grouping in case a timezone has different sleeping habits; which might be novel to investigate), it seems like the hour relative to daylight is a more important feature than normalizing the times.\n\nThink  of it this way, for those with a sleep cycle based on day-night, it's more important to know the relative hour in terms of daylight (for example 8pm when truncating the timezone should have relatively similar amounts of daylight, so it's more of an apples-to-apples comparison, whereas a UTC conversion loses this detail).",
          "votes": 2,
          "replies": [
            {
              "id": 2433492,
              "postDate": "2023-09-11T16:03:01.440Z",
              "rawMarkdown": "",
              "isDeleted": true
            },
            {
              "id": 2453438,
              "postDate": "2023-09-24T03:41:09.693Z",
              "content": "<p>Hi <a href=\"https://www.kaggle.com/rknuuds\" target=\"_blank\">@rknuuds</a> ,</p>\n<p>I also agree with you.<br>\nBut in new data update they have removed the time zone information completely.</p>",
              "rawMarkdown": "Hi @rknuuds ,\n\nI also agree with you.\nBut in new data update they have removed the time zone information completely."
            }
          ]
        }
      ]
    },
    {
      "id": 2432028,
      "postDate": "2023-09-10T15:00:22.043Z",
      "content": "<p>Hi Andrada,</p>\n<p>Though Carl and Lucas already answered your issue (Parsing Timestamp). </p>\n<p>It's always nice to have a plan B (sometimes C, D too : )<br>\nThat's what I found on this competition: </p>\n<p>Viz of sleeping time series By Chun Fu<br>\n<a href=\"https://www.kaggle.com/code/patrick0302/viz-of-sleeping-time-series\" target=\"_blank\">https://www.kaggle.com/code/patrick0302/viz-of-sleeping-time-series</a></p>\n<p>CMI Sleep Visualization - By Connor<br>\n<a href=\"https://www.kaggle.com/code/clane9/cmi-sleep-visualization\" target=\"_blank\">https://www.kaggle.com/code/clane9/cmi-sleep-visualization</a></p>\n<p>Sleeping patterns - EDA - By Robert Kwiatkowski<br>\n<a href=\"https://www.kaggle.com/code/datark1/sleeping-patterns-eda\" target=\"_blank\">https://www.kaggle.com/code/datark1/sleeping-patterns-eda</a></p>\n<p>Xgboost Notebook Updated - By Anmar Ahmadi Qazi<br>\n<a href=\"https://www.kaggle.com/code/ammarahmadqazi/xgboost-notebook-updated\" target=\"_blank\">https://www.kaggle.com/code/ammarahmadqazi/xgboost-notebook-updated</a></p>\n<p>204_Preprocessing_Data - By Alvaro Rodriguez<br>\n<a href=\"https://www.kaggle.com/code/alvrod/204-preprocessing-data\" target=\"_blank\">https://www.kaggle.com/code/alvrod/204-preprocessing-data</a></p>\n<p>Kind Regards,<br>\nMarília.</p>",
      "rawMarkdown": "Hi Andrada,\n\nThough Carl and Lucas already answered your issue (Parsing Timestamp). \n\nIt's always nice to have a plan B (sometimes C, D too : )\nThat's what I found on this competition: \n\nViz of sleeping time series By Chun Fu\nhttps://www.kaggle.com/code/patrick0302/viz-of-sleeping-time-series\n\nCMI Sleep Visualization - By Connor\nhttps://www.kaggle.com/code/clane9/cmi-sleep-visualization\n\nSleeping patterns - EDA - By Robert Kwiatkowski\nhttps://www.kaggle.com/code/datark1/sleeping-patterns-eda\n\nXgboost Notebook Updated - By Anmar Ahmadi Qazi\nhttps://www.kaggle.com/code/ammarahmadqazi/xgboost-notebook-updated\n\n204_Preprocessing_Data - By Alvaro Rodriguez\nhttps://www.kaggle.com/code/alvrod/204-preprocessing-data\n\nKind Regards,\nMarília.",
      "votes": 3
    },
    {
      "id": 2431135,
      "postDate": "2023-09-09T20:05:56.247Z",
      "content": "<p>The mix of timezones (due to DST) causes problems for pandas.  Small samples are less likely to encounter this problem</p>\n<p>From the <a href=\"https://pandas.pydata.org/docs/reference/api/pandas.to_datetime.html\" target=\"_blank\">Pandas documentation</a>:</p>\n<blockquote>\n  <p>The following causes are responsible for datetime.datetime <strong>objects being returned</strong> (possibly inside an Index or a Series with object dtype) <strong>instead of a proper pandas designated type</strong> (Timestamp, DatetimeIndex or Series with datetime64 dtype):<br>\n  …</p>\n  <ul>\n  <li>when utc=False (default) and the input is an array-like or Series containing mixed naive/aware datetime, or aware with mixed time offsets. <em>Note that this happens in the (quite frequent) situation when the timezone has a daylight savings policy.</em> In that case you may wish to use utc=True.</li>\n  </ul>\n</blockquote>",
      "rawMarkdown": "The mix of timezones (due to DST) causes problems for pandas.  Small samples are less likely to encounter this problem\n\nFrom the [Pandas documentation](https://pandas.pydata.org/docs/reference/api/pandas.to_datetime.html):\n>The following causes are responsible for datetime.datetime **objects being returned** (possibly inside an Index or a Series with object dtype) **instead of a proper pandas designated type** (Timestamp, DatetimeIndex or Series with datetime64 dtype):\n>...\n>- when utc=False (default) and the input is an array-like or Series containing mixed naive/aware datetime, or aware with mixed time offsets. *Note that this happens in the (quite frequent) situation when the timezone has a daylight savings policy.* In that case you may wish to use utc=True.",
      "votes": 4
    },
    {
      "id": 2453442,
      "postDate": "2023-09-24T03:51:29.340Z",
      "content": "<p>Hi <a href=\"https://www.kaggle.com/andradaolteanu\" target=\"_blank\">@andradaolteanu</a> ,<br>\nIts little late but if it helps, I am sharing my observations.</p>\n<p>I found that operating on whole column is faster that pandarellel parallel_apply</p>\n<p>using function once returned column with dtype object.<br>\nSomehow applying the same function twice worked for me</p>\n<p>train_events[\"timestamp\"] = pd.to_datetime(train_events[\"timestamp\"],errors = \"coerce\",format='ISO8601')<br>\ntrain_events[\"timestamp\"] = pd.to_datetime(train_events[\"timestamp\"],errors = \"coerce\",format='ISO8601')</p>\n<p>this changed column dtype to datetime </p>\n<p>I don't know, if this is some kind of bug or some intricate pandas thing.</p>\n<p>the quoted code lines are from following notebook by me :</p>\n<p><a href=\"https://www.kaggle.com/code/rohitchaudhari25/cmi-dss-23-94-memory-reduction-20-lines-of-code\" target=\"_blank\">CMI-DSS-23 94%-memory reduction | 20-lines of code</a></p>",
      "rawMarkdown": "Hi @andradaolteanu ,\nIts little late but if it helps, I am sharing my observations.\n\nI found that operating on whole column is faster that pandarellel parallel_apply\n\nusing function once returned column with dtype object.\nSomehow applying the same function twice worked for me\n\ntrain_events[\"timestamp\"] = pd.to_datetime(train_events[\"timestamp\"],errors = \"coerce\",format='ISO8601')\ntrain_events[\"timestamp\"] = pd.to_datetime(train_events[\"timestamp\"],errors = \"coerce\",format='ISO8601')\n\nthis changed column dtype to datetime \n\nI don't know, if this is some kind of bug or some intricate pandas thing.\n\nthe quoted code lines are from following notebook by me :\n\n[CMI-DSS-23 94%-memory reduction | 20-lines of code](https://www.kaggle.com/code/rohitchaudhari25/cmi-dss-23-94-memory-reduction-20-lines-of-code)"
    },
    {
      "id": 2433555,
      "postDate": "2023-09-11T16:49:05.700Z",
      "content": "<p>Hello <a href=\"https://www.kaggle.com/andradaolteanu\" target=\"_blank\">@andradaolteanu</a></p>\n<p>I did not have any problems using the format specified on the data page: <a href=\"https://www.kaggle.com/competitions/child-mind-institute-detect-sleep-states/data\" target=\"_blank\">https://www.kaggle.com/competitions/child-mind-institute-detect-sleep-states/data</a></p>\n<pre><code>train_series_df[] = pd.to_datetime(\n    train_series_df[],\n    =\n)\n</code></pre>\n<p><em>NOTE: Because of the size of the dataset, converting the timestamp column in the entire dataset could be super slow.</em></p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F5210536%2F8b35f0c752582ffef5c44dbeeef5da90%2FScreenshot%202023-09-11%20at%2021.34.50.png?generation=1694450884718531&amp;alt=media\" alt=\"\"></p>",
      "rawMarkdown": "Hello @andradaolteanu\n\nI did not have any problems using the format specified on the data page: https://www.kaggle.com/competitions/child-mind-institute-detect-sleep-states/data\n\n```python\ntrain_series_df['timestamp'] = pd.to_datetime(\n    train_series_df['timestamp'],\n    format='%Y-%m-%dT%H:%M:%S%z'\n)\n```\n\n_NOTE: Because of the size of the dataset, converting the timestamp column in the entire dataset could be super slow._\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F5210536%2F8b35f0c752582ffef5c44dbeeef5da90%2FScreenshot%202023-09-11%20at%2021.34.50.png?generation=1694450884718531&alt=media)",
      "votes": 2,
      "isDeleted": true
    },
    {
      "id": 2431333,
      "postDate": "2023-09-10T03:39:52.573Z",
      "rawMarkdown": "",
      "votes": -6,
      "isDeleted": true
    }
  ],
  "comments": [
    {
      "id": 2431106,
      "author_name": "Carl McBride Ellis",
      "author_url": "",
      "post_date": "2023-09-09T19:33:18.903000",
      "content": "<p>Dear <a href=\"https://www.kaggle.com/andradaolteanu\" target=\"_blank\">@andradaolteanu</a> </p>\n<p>Perhaps try </p>\n<pre><code>df[] = pd.to_datetime(df[],utc=)\n</code></pre>\n<p>All the best,<br>\ncarl</p>",
      "votes": 8,
      "replies": [
        {
          "id": 2431755,
          "author_name": "Andrada",
          "author_url": "",
          "post_date": "2023-09-10T11:29:13.610000",
          "content": "<p>YES IT WORKED! Thank you! ❤️</p>",
          "votes": 2,
          "replies": []
        },
        {
          "id": 2432927,
          "author_name": "jagofc",
          "author_url": "",
          "post_date": "2023-09-11T08:03:40.287000",
          "content": "<p><a href=\"https://www.kaggle.com/carlmcbrideellis\" target=\"_blank\">@carlmcbrideellis</a> - your suggestion works, but it will give UTC, which may give an inconsistent picture across different geographical locations.</p>\n<p>To get local time one can do:</p>\n<pre><code>df[] = pd.to_datetime(df[]).apply( t: t.tz_localize())\n</code></pre>\n<p>Note that one needs to <code>apply</code> localisation, because that function cannot vectorise across different timezones.</p>",
          "votes": 7,
          "replies": [
            {
              "id": 2433594,
              "author_name": "Carl McBride Ellis",
              "author_url": "",
              "post_date": "2023-09-11T17:10:40.577000",
              "content": "<p>Dear <a href=\"https://www.kaggle.com/jagofc\" target=\"_blank\">@jagofc</a> </p>\n<p>Good point! <br>\n(Shame that using an <code>apply</code> is sooo slowwww!)</p>\n<p>All the best,<br>\ncarl</p>",
              "votes": 1,
              "replies": []
            },
            {
              "id": 2433844,
              "author_name": "",
              "author_url": "",
              "post_date": "2023-09-11T22:08:29.913000",
              "content": "",
              "votes": 4,
              "replies": []
            },
            {
              "id": 2434272,
              "author_name": "jagofc",
              "author_url": "",
              "post_date": "2023-09-12T07:42:17.347000",
              "content": "<p><a href=\"https://www.kaggle.com/weka511\" target=\"_blank\">@weka511</a> - it's true. Just my take - I'm using <code>step</code> as the unique indexer, and local time just as a source of features. Also nothing wrong with preserving <code>timestamp</code> (utc as per <a href=\"https://www.kaggle.com/carlmcbrideellis\" target=\"_blank\">@carlmcbrideellis</a>) as an indexer and defining a new column <code>timestamp_local</code> for local time features instead of overwriting <code>timestamp</code>.</p>",
              "votes": 2,
              "replies": []
            },
            {
              "id": 2434380,
              "author_name": "",
              "author_url": "",
              "post_date": "2023-09-12T09:06:12.340000",
              "content": "",
              "votes": 3,
              "replies": []
            },
            {
              "id": 2434438,
              "author_name": "Carl McBride Ellis",
              "author_url": "",
              "post_date": "2023-09-12T09:47:23.117000",
              "content": "<p>Dear <a href=\"https://www.kaggle.com/jagofc\" target=\"_blank\">@jagofc</a>  and <a href=\"https://www.kaggle.com/weka511\" target=\"_blank\">@weka511</a> </p>\n<p>Solved: </p>\n<pre><code>df[] = pd.to_datetime(df[]).apply( t: t.tz_localize())\n</code></pre>\n<p>is indeed the way too go; see my trivial test here: <a href=\"https://www.kaggle.com/code/carlmcbrideellis/zzzs-eda-utc-or-no-utc\" target=\"_blank\">\"Zzzs: EDA: UTC or no UTC\"</a></p>\n<p>Using UTC:</p>\n<p>Wake in the morning:         3925<br>\nGo to sleep in the morning:  4693<br>\nSleep in the afternoon:      100<br>\nWake in the afternoon:       869</p>\n<p>Using local</p>\n<p>Wake in the morning:         4760<br>\nGo to sleep in the morning:  956<br>\nSleep in the afternoon:      3837<br>\nWake in the afternoon:       34</p>\n<p>All the best!<br>\ncarl</p>",
              "votes": 3,
              "replies": []
            }
          ]
        }
      ]
    },
    {
      "id": 2431139,
      "author_name": "Lucas Morin",
      "author_url": "",
      "post_date": "2023-09-09T20:32:04.227000",
      "content": "<p>There is a timezone at the end of the timestamp in string format, that doesn't seem to be recognized by pd.todatetime.<br>\nOne work around seems to be to cut the string to remove the timezone. (maybe it should be kept elsewhere for modelling).</p>\n<pre><code> datetime\ntargets = pd.read_csv()\ntargets.timestamp = targets.timestamp.[:-]\ntargets.timestamp = pd.to_datetime(targets.timestamp)\n</code></pre>",
      "votes": 6,
      "replies": [
        {
          "id": 2433428,
          "author_name": "Rob K.",
          "author_url": "",
          "post_date": "2023-09-11T15:28:28.623000",
          "content": "<p>I second this.  Since this has to deal with sleep cycles, the timezone (while might be significant for regional grouping in case a timezone has different sleeping habits; which might be novel to investigate), it seems like the hour relative to daylight is a more important feature than normalizing the times.</p>\n<p>Think  of it this way, for those with a sleep cycle based on day-night, it's more important to know the relative hour in terms of daylight (for example 8pm when truncating the timezone should have relatively similar amounts of daylight, so it's more of an apples-to-apples comparison, whereas a UTC conversion loses this detail).</p>",
          "votes": 2,
          "replies": [
            {
              "id": 2433492,
              "author_name": "",
              "author_url": "",
              "post_date": "2023-09-11T16:03:01.440000",
              "content": "",
              "votes": 0,
              "replies": []
            },
            {
              "id": 2453438,
              "author_name": "Rohit Chaudhari",
              "author_url": "",
              "post_date": "2023-09-24T03:41:09.693000",
              "content": "<p>Hi <a href=\"https://www.kaggle.com/rknuuds\" target=\"_blank\">@rknuuds</a> ,</p>\n<p>I also agree with you.<br>\nBut in new data update they have removed the time zone information completely.</p>",
              "votes": 0,
              "replies": []
            }
          ]
        }
      ]
    },
    {
      "id": 2432028,
      "author_name": "Marília Prata",
      "author_url": "",
      "post_date": "2023-09-10T15:00:22.043000",
      "content": "<p>Hi Andrada,</p>\n<p>Though Carl and Lucas already answered your issue (Parsing Timestamp). </p>\n<p>It's always nice to have a plan B (sometimes C, D too : )<br>\nThat's what I found on this competition: </p>\n<p>Viz of sleeping time series By Chun Fu<br>\n<a href=\"https://www.kaggle.com/code/patrick0302/viz-of-sleeping-time-series\" target=\"_blank\">https://www.kaggle.com/code/patrick0302/viz-of-sleeping-time-series</a></p>\n<p>CMI Sleep Visualization - By Connor<br>\n<a href=\"https://www.kaggle.com/code/clane9/cmi-sleep-visualization\" target=\"_blank\">https://www.kaggle.com/code/clane9/cmi-sleep-visualization</a></p>\n<p>Sleeping patterns - EDA - By Robert Kwiatkowski<br>\n<a href=\"https://www.kaggle.com/code/datark1/sleeping-patterns-eda\" target=\"_blank\">https://www.kaggle.com/code/datark1/sleeping-patterns-eda</a></p>\n<p>Xgboost Notebook Updated - By Anmar Ahmadi Qazi<br>\n<a href=\"https://www.kaggle.com/code/ammarahmadqazi/xgboost-notebook-updated\" target=\"_blank\">https://www.kaggle.com/code/ammarahmadqazi/xgboost-notebook-updated</a></p>\n<p>204_Preprocessing_Data - By Alvaro Rodriguez<br>\n<a href=\"https://www.kaggle.com/code/alvrod/204-preprocessing-data\" target=\"_blank\">https://www.kaggle.com/code/alvrod/204-preprocessing-data</a></p>\n<p>Kind Regards,<br>\nMarília.</p>",
      "votes": 3,
      "replies": []
    },
    {
      "id": 2431135,
      "author_name": "Quillen",
      "author_url": "",
      "post_date": "2023-09-09T20:05:56.247000",
      "content": "<p>The mix of timezones (due to DST) causes problems for pandas.  Small samples are less likely to encounter this problem</p>\n<p>From the <a href=\"https://pandas.pydata.org/docs/reference/api/pandas.to_datetime.html\" target=\"_blank\">Pandas documentation</a>:</p>\n<blockquote>\n  <p>The following causes are responsible for datetime.datetime <strong>objects being returned</strong> (possibly inside an Index or a Series with object dtype) <strong>instead of a proper pandas designated type</strong> (Timestamp, DatetimeIndex or Series with datetime64 dtype):<br>\n  …</p>\n  <ul>\n  <li>when utc=False (default) and the input is an array-like or Series containing mixed naive/aware datetime, or aware with mixed time offsets. <em>Note that this happens in the (quite frequent) situation when the timezone has a daylight savings policy.</em> In that case you may wish to use utc=True.</li>\n  </ul>\n</blockquote>",
      "votes": 4,
      "replies": []
    },
    {
      "id": 2453442,
      "author_name": "Rohit Chaudhari",
      "author_url": "",
      "post_date": "2023-09-24T03:51:29.340000",
      "content": "<p>Hi <a href=\"https://www.kaggle.com/andradaolteanu\" target=\"_blank\">@andradaolteanu</a> ,<br>\nIts little late but if it helps, I am sharing my observations.</p>\n<p>I found that operating on whole column is faster that pandarellel parallel_apply</p>\n<p>using function once returned column with dtype object.<br>\nSomehow applying the same function twice worked for me</p>\n<p>train_events[\"timestamp\"] = pd.to_datetime(train_events[\"timestamp\"],errors = \"coerce\",format='ISO8601')<br>\ntrain_events[\"timestamp\"] = pd.to_datetime(train_events[\"timestamp\"],errors = \"coerce\",format='ISO8601')</p>\n<p>this changed column dtype to datetime </p>\n<p>I don't know, if this is some kind of bug or some intricate pandas thing.</p>\n<p>the quoted code lines are from following notebook by me :</p>\n<p><a href=\"https://www.kaggle.com/code/rohitchaudhari25/cmi-dss-23-94-memory-reduction-20-lines-of-code\" target=\"_blank\">CMI-DSS-23 94%-memory reduction | 20-lines of code</a></p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 2433555,
      "author_name": "Bhargav Kowshik",
      "author_url": "",
      "post_date": "2023-09-11T16:49:05.700000",
      "content": "<p>Hello <a href=\"https://www.kaggle.com/andradaolteanu\" target=\"_blank\">@andradaolteanu</a></p>\n<p>I did not have any problems using the format specified on the data page: <a href=\"https://www.kaggle.com/competitions/child-mind-institute-detect-sleep-states/data\" target=\"_blank\">https://www.kaggle.com/competitions/child-mind-institute-detect-sleep-states/data</a></p>\n<pre><code>train_series_df[] = pd.to_datetime(\n    train_series_df[],\n    =\n)\n</code></pre>\n<p><em>NOTE: Because of the size of the dataset, converting the timestamp column in the entire dataset could be super slow.</em></p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F5210536%2F8b35f0c752582ffef5c44dbeeef5da90%2FScreenshot%202023-09-11%20at%2021.34.50.png?generation=1694450884718531&amp;alt=media\" alt=\"\"></p>",
      "votes": 2,
      "replies": []
    },
    {
      "id": 2431333,
      "author_name": "",
      "author_url": "",
      "post_date": "2023-09-10T03:39:52.573000",
      "content": "",
      "votes": -6,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "2431100": "Hi all!\n\nI have a weird problem and I do not understand what am I missing.\n\nWithin the `train_series` dataset I would like to convert the `timestamp` variable from dtype object to dtype datetime.\n\nIf I test my function on a small piece of data, it returns something like this:\n<img src=\"https://i.imgur.com/4pZ5Sv4.png\", width=500>\n\nAll looks good. But when I try to apply the same code on the entire 140mil rows, the `timestamp` column does not convert. Moreover, it shows that the allocated memory for this column is bigger.\n\nI tried randomly sampling the data and take 10k rows and it's the same story, the column does not convert.\n<img src=\"https://i.imgur.com/bXzjmLi.png\", width=500>\n\nCan anybody explain me why is this happening? And maybe if there is a fix to this?\n\nThank you and happy day ^^",
    "2431106": "Dear @andradaolteanu \n\nPerhaps try \n\n```python\ndf[\"timestamp\"] = pd.to_datetime(df[\"timestamp\"],utc=True)\n```\n\nAll the best,\ncarl",
    "2431139": "There is a timezone at the end of the timestamp in string format, that doesn't seem to be recognized by pd.todatetime.\nOne work around seems to be to cut the string to remove the timezone. (maybe it should be kept elsewhere for modelling).\n\n```python\nimport datetime\ntargets = pd.read_csv('/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv')\ntargets.timestamp = targets.timestamp.str[:-8]\ntargets.timestamp = pd.to_datetime(targets.timestamp)\n```",
    "2432028": "Hi Andrada,\n\nThough Carl and Lucas already answered your issue (Parsing Timestamp). \n\nIt's always nice to have a plan B (sometimes C, D too : )\nThat's what I found on this competition: \n\nViz of sleeping time series By Chun Fu\nhttps://www.kaggle.com/code/patrick0302/viz-of-sleeping-time-series\n\nCMI Sleep Visualization - By Connor\nhttps://www.kaggle.com/code/clane9/cmi-sleep-visualization\n\nSleeping patterns - EDA - By Robert Kwiatkowski\nhttps://www.kaggle.com/code/datark1/sleeping-patterns-eda\n\nXgboost Notebook Updated - By Anmar Ahmadi Qazi\nhttps://www.kaggle.com/code/ammarahmadqazi/xgboost-notebook-updated\n\n204_Preprocessing_Data - By Alvaro Rodriguez\nhttps://www.kaggle.com/code/alvrod/204-preprocessing-data\n\nKind Regards,\nMarília.",
    "2431135": "The mix of timezones (due to DST) causes problems for pandas.  Small samples are less likely to encounter this problem\n\nFrom the [Pandas documentation](https://pandas.pydata.org/docs/reference/api/pandas.to_datetime.html):\n>The following causes are responsible for datetime.datetime **objects being returned** (possibly inside an Index or a Series with object dtype) **instead of a proper pandas designated type** (Timestamp, DatetimeIndex or Series with datetime64 dtype):\n>...\n>- when utc=False (default) and the input is an array-like or Series containing mixed naive/aware datetime, or aware with mixed time offsets. *Note that this happens in the (quite frequent) situation when the timezone has a daylight savings policy.* In that case you may wish to use utc=True.",
    "2453442": "Hi @andradaolteanu ,\nIts little late but if it helps, I am sharing my observations.\n\nI found that operating on whole column is faster that pandarellel parallel_apply\n\nusing function once returned column with dtype object.\nSomehow applying the same function twice worked for me\n\ntrain_events[\"timestamp\"] = pd.to_datetime(train_events[\"timestamp\"],errors = \"coerce\",format='ISO8601')\ntrain_events[\"timestamp\"] = pd.to_datetime(train_events[\"timestamp\"],errors = \"coerce\",format='ISO8601')\n\nthis changed column dtype to datetime \n\nI don't know, if this is some kind of bug or some intricate pandas thing.\n\nthe quoted code lines are from following notebook by me :\n\n[CMI-DSS-23 94%-memory reduction | 20-lines of code](https://www.kaggle.com/code/rohitchaudhari25/cmi-dss-23-94-memory-reduction-20-lines-of-code)",
    "2433555": "Hello @andradaolteanu\n\nI did not have any problems using the format specified on the data page: https://www.kaggle.com/competitions/child-mind-institute-detect-sleep-states/data\n\n```python\ntrain_series_df['timestamp'] = pd.to_datetime(\n    train_series_df['timestamp'],\n    format='%Y-%m-%dT%H:%M:%S%z'\n)\n```\n\n_NOTE: Because of the size of the dataset, converting the timestamp column in the entire dataset could be super slow._\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F5210536%2F8b35f0c752582ffef5c44dbeeef5da90%2FScreenshot%202023-09-11%20at%2021.34.50.png?generation=1694450884718531&alt=media)",
    "2431333": ""
  }
}