{
  "id": 338306,
  "title": "Python code to convert date string to date object in Goodreads competition",
  "url": "/competitions/goodreads-books-reviews-290312/discussion/338306",
  "author_name": "",
  "post_date": "2022-07-20T03:19:03.615132900Z",
  "votes": 14,
  "comment_count": 6,
  "views": 0,
  "content": "<blockquote>\n  <p>I used only 50k entries to convert to TFIDF and reduced the result to 1000 features with TruncSVD. Then training and predicting with simple LogReg gave me 0.49161. Now I'll try to add other features to the model.</p>\n</blockquote>\n<p>This is a format to convert date string to datetime object:<br>\n<code>datetime.strptime('Sat Mar 18 23:22:42 -0700 2017', '%a %b %d %H:%M:%S %z %Y')</code></p>\n<p>Use it to map like that<br>\n<code>train['read_at'] = train['read_at'].apply(lambda x: datetime.strptime(x, '%a %b %d %H:%M:%S %z %Y'))</code></p>",
  "messages": [
    {
      "id": "1862822",
      "postDate": "07/20/2022 03:19:03",
      "content": "<blockquote>\n  <p>I used only 50k entries to convert to TFIDF and reduced the result to 1000 features with TruncSVD. Then training and predicting with simple LogReg gave me 0.49161. Now I'll try to add other features to the model.</p>\n</blockquote>\n<p>This is a format to convert date string to datetime object:<br>\n<code>datetime.strptime('Sat Mar 18 23:22:42 -0700 2017', '%a %b %d %H:%M:%S %z %Y')</code></p>\n<p>Use it to map like that<br>\n<code>train['read_at'] = train['read_at'].apply(lambda x: datetime.strptime(x, '%a %b %d %H:%M:%S %z %Y'))</code></p>",
      "rawMarkdown": "> I used only 50k entries to convert to TFIDF and reduced the result to 1000 features with TruncSVD. Then training and predicting with simple LogReg gave me 0.49161. Now I'll try to add other features to the model.\n\n This is a format to convert date string to datetime object:\n`datetime.strptime('Sat Mar 18 23:22:42 -0700 2017', '%a %b %d %H:%M:%S %z %Y')`\n\nUse it to map like that\n`train['read_at'] = train['read_at'].apply(lambda x: datetime.strptime(x, '%a %b %d %H:%M:%S %z %Y'))`",
      "votes": null
    },
    {
      "id": "1869533",
      "postDate": "07/24/2022 20:21:22",
      "content": "<p>How would you like to use the date? read_at</p>",
      "rawMarkdown": "How would you like to use the date? read_at",
      "votes": null
    },
    {
      "id": "1870294",
      "postDate": "07/25/2022 13:02:05",
      "content": "<p>Well from \"started_at\" - \"read_at\" you can have a users's book reading time in days! </p>\n<p>Going further, you can compare it to the average that tooks a user to read this book, or this particular user to read any book.<br>\nI haven't tested this approach yet.</p>",
      "rawMarkdown": "Well from \"started_at\" - \"read_at\" you can have a users's book reading time in days! \n\nGoing further, you can compare it to the average that tooks a user to read this book, or this particular user to read any book.\nI haven't tested this approach yet.",
      "votes": null
    },
    {
      "id": "1905439",
      "postDate": "08/19/2022 04:34:17",
      "content": "<p>Just a thing to say.<br>\nIn mine, I have to write ' datetime.datetime.strptime('Sat Mar 18 23:22:42 -0700 2017', '%a %b %d %H:%M:%S %z %Y') '<br>\nElse thank you.</p>",
      "rawMarkdown": "Just a thing to say.\nIn mine, I have to write ' datetime.datetime.strptime('Sat Mar 18 23:22:42 -0700 2017', '%a %b %d %H:%M:%S %z %Y') '\nElse thank you.",
      "votes": null
    },
    {
      "id": "2071622",
      "postDate": "12/21/2022 08:22:45",
      "content": "<p>DATE_FORMAT = '%a %b %d %H:%M:%S %z %Y'<br>\ndef parse_date(date_str):<br>\n    if (date_str != date_str) or (date_str is None):<br>\n        return None<br>\n    if len(date_str.split()) != 6:<br>\n        return None<br>\n    else:<br>\n        try:<br>\n            return datetime.datetime.strptime(date_str, DATE_FORMAT)<br>\n        except Exception as e:<br>\n            print(date_str, e)        <br>\n        return None</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1188724%2Fd8ce78c1e3653b95d59ae6592da98e94%2Ftemp.png?generation=1671611199897746&amp;alt=media\" alt=\"\"></p>",
      "rawMarkdown": "DATE_FORMAT = '%a %b %d %H:%M:%S %z %Y'\ndef parse_date(date_str):\n    if (date_str != date_str) or (date_str is None):\n        return None\n    if len(date_str.split()) != 6:\n        return None\n    else:\n        try:\n            return datetime.datetime.strptime(date_str, DATE_FORMAT)\n        except Exception as e:\n            print(date_str, e)        \n        return None\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1188724%2Fd8ce78c1e3653b95d59ae6592da98e94%2Ftemp.png?generation=1671611199897746&alt=media)",
      "votes": null
    },
    {
      "id": "2094064",
      "postDate": "01/10/2023 15:36:22",
      "content": "<p>There is a very convenient way of doing this in pandas:</p>\n<pre><code>train_df = pd(train_df.started_at) \ntrain_df = pd(train_df.read_at) \ntrain_df = train_df - train_df \ntrain_df = train_df \n</code></pre>",
      "rawMarkdown": "There is a very convenient way of doing this in pandas:\n```{python}\ntrain_df['started_at_dt'] = pd.to_datetime(train_df.started_at) \ntrain_df['read_at_dt'] = pd.to_datetime(train_df.read_at) \ntrain_df['duration_dt'] = train_df.read_at_dt - train_df.started_at_dt \ntrain_df['read_length_days'] = train_df.duration_dt.dt.days \n```",
      "votes": null
    },
    {
      "id": "2097706",
      "postDate": "01/12/2023 21:41:41",
      "content": "<blockquote>\n  <p>Well from \"started_at\" - \"read_at\" you can have a users's book reading time in days! </p>\n  <p>Going further, you can compare it to the average that tooks a user to read this book, or this particular user to read any book.<br>\n  I haven't tested this approach yet.</p>\n</blockquote>\n<p><a href=\"https://www.kaggle.com/willingtodev\" target=\"_blank\">@willingtodev</a> Does started_at literally mean the date when the user started reading that date ? I am seeing quite a bunch of cases where started_at &lt; read_at 🙄</p>",
      "rawMarkdown": "> Well from \"started_at\" - \"read_at\" you can have a users's book reading time in days! \n> \n> Going further, you can compare it to the average that tooks a user to read this book, or this particular user to read any book.\n> I haven't tested this approach yet.\n\n@willingtodev Does started_at literally mean the date when the user started reading that date ? I am seeing quite a bunch of cases where started_at < read_at 🙄",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1869533,
      "author_name": "mikhailutrobin",
      "author_url": "",
      "post_date": "07/24/2022 20:21:22",
      "content": "<p>How would you like to use the date? read_at</p>",
      "votes": null,
      "replies": [
        {
          "id": 1870294,
          "author_name": "willingtodev",
          "author_url": "",
          "post_date": "07/25/2022 13:02:05",
          "content": "<p>Well from \"started_at\" - \"read_at\" you can have a users's book reading time in days! </p>\n<p>Going further, you can compare it to the average that tooks a user to read this book, or this particular user to read any book.<br>\nI haven't tested this approach yet.</p>",
          "votes": null,
          "replies": [
            {
              "id": 2094064,
              "author_name": "chimael",
              "author_url": "",
              "post_date": "01/10/2023 15:36:22",
              "content": "<p>There is a very convenient way of doing this in pandas:</p>\n<pre><code>train_df = pd(train_df.started_at) \ntrain_df = pd(train_df.read_at) \ntrain_df = train_df - train_df \ntrain_df = train_df \n</code></pre>",
              "votes": null,
              "replies": []
            },
            {
              "id": 2097706,
              "author_name": "poludniowyalbanczyk",
              "author_url": "",
              "post_date": "01/12/2023 21:41:41",
              "content": "<blockquote>\n  <p>Well from \"started_at\" - \"read_at\" you can have a users's book reading time in days! </p>\n  <p>Going further, you can compare it to the average that tooks a user to read this book, or this particular user to read any book.<br>\n  I haven't tested this approach yet.</p>\n</blockquote>\n<p><a href=\"https://www.kaggle.com/willingtodev\" target=\"_blank\">@willingtodev</a> Does started_at literally mean the date when the user started reading that date ? I am seeing quite a bunch of cases where started_at &lt; read_at 🙄</p>",
              "votes": null,
              "replies": []
            }
          ]
        }
      ]
    },
    {
      "id": 1905439,
      "author_name": "saipradhan",
      "author_url": "",
      "post_date": "08/19/2022 04:34:17",
      "content": "<p>Just a thing to say.<br>\nIn mine, I have to write ' datetime.datetime.strptime('Sat Mar 18 23:22:42 -0700 2017', '%a %b %d %H:%M:%S %z %Y') '<br>\nElse thank you.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 2071622,
      "author_name": "yash1158",
      "author_url": "",
      "post_date": "12/21/2022 08:22:45",
      "content": "<p>DATE_FORMAT = '%a %b %d %H:%M:%S %z %Y'<br>\ndef parse_date(date_str):<br>\n    if (date_str != date_str) or (date_str is None):<br>\n        return None<br>\n    if len(date_str.split()) != 6:<br>\n        return None<br>\n    else:<br>\n        try:<br>\n            return datetime.datetime.strptime(date_str, DATE_FORMAT)<br>\n        except Exception as e:<br>\n            print(date_str, e)        <br>\n        return None</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1188724%2Fd8ce78c1e3653b95d59ae6592da98e94%2Ftemp.png?generation=1671611199897746&amp;alt=media\" alt=\"\"></p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1862822": "> I used only 50k entries to convert to TFIDF and reduced the result to 1000 features with TruncSVD. Then training and predicting with simple LogReg gave me 0.49161. Now I'll try to add other features to the model.\n\n This is a format to convert date string to datetime object:\n`datetime.strptime('Sat Mar 18 23:22:42 -0700 2017', '%a %b %d %H:%M:%S %z %Y')`\n\nUse it to map like that\n`train['read_at'] = train['read_at'].apply(lambda x: datetime.strptime(x, '%a %b %d %H:%M:%S %z %Y'))`",
    "1869533": "How would you like to use the date? read_at",
    "1870294": "Well from \"started_at\" - \"read_at\" you can have a users's book reading time in days! \n\nGoing further, you can compare it to the average that tooks a user to read this book, or this particular user to read any book.\nI haven't tested this approach yet.",
    "1905439": "Just a thing to say.\nIn mine, I have to write ' datetime.datetime.strptime('Sat Mar 18 23:22:42 -0700 2017', '%a %b %d %H:%M:%S %z %Y') '\nElse thank you.",
    "2071622": "DATE_FORMAT = '%a %b %d %H:%M:%S %z %Y'\ndef parse_date(date_str):\n    if (date_str != date_str) or (date_str is None):\n        return None\n    if len(date_str.split()) != 6:\n        return None\n    else:\n        try:\n            return datetime.datetime.strptime(date_str, DATE_FORMAT)\n        except Exception as e:\n            print(date_str, e)        \n        return None\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1188724%2Fd8ce78c1e3653b95d59ae6592da98e94%2Ftemp.png?generation=1671611199897746&alt=media)",
    "2094064": "There is a very convenient way of doing this in pandas:\n```{python}\ntrain_df['started_at_dt'] = pd.to_datetime(train_df.started_at) \ntrain_df['read_at_dt'] = pd.to_datetime(train_df.read_at) \ntrain_df['duration_dt'] = train_df.read_at_dt - train_df.started_at_dt \ntrain_df['read_length_days'] = train_df.duration_dt.dt.days \n```",
    "2097706": "> Well from \"started_at\" - \"read_at\" you can have a users's book reading time in days! \n> \n> Going further, you can compare it to the average that tooks a user to read this book, or this particular user to read any book.\n> I haven't tested this approach yet.\n\n@willingtodev Does started_at literally mean the date when the user started reading that date ? I am seeing quite a bunch of cases where started_at < read_at 🙄"
  },
  "source": "meta"
}