[PYTHON] I tried various patterns of date strings to be entered in pandas.to_datetime

Introduction

Python practice data analysis 100 knocks I was dealing with date data on pandas. I used pandas.to_datetime () to convert the date of the string to the date type data, but I converted it properly without showing the format etc. and it worked. I wasn't sure what format would work, so I checked it.

Tried environment

Successful pattern

>>> import pandas as pd
>>> from datetime import datetime

#Pattern separated by a hyphen
>>> pd.to_datetime("2019-12-31")
Timestamp('2019-12-31 00:00:00')

# datetime.today()OK
>>> pd.to_datetime(datetime.today())
Timestamp('2019-12-31 09:56:05.590997')

#Dates are OK without delimiters
>>> pd.to_datetime("20191231 13:00")
Timestamp('2019-12-31 13:00:00')
>>> pd.to_datetime("20191231")
Timestamp('2019-12-31 00:00:00')

#If it is before 2000, the order of year, month, and day in the second digit of the Christian era is OK, separated by slashes.
>>> pd.to_datetime("83/12/31")
Timestamp('1983-12-31 00:00:00')

#Of course, future dates are OK
>>> pd.to_datetime("2038/12/31 12:35:56.55457")
Timestamp('2038-12-31 12:35:56.554570')

Patterns that don't work

#If only dates separated by slashes are used, they will be interpreted as days, months, and years in Western style.
>>> pd.to_datetime("19/12/31")
Timestamp('2031-12-19 00:00:00')

Summary

If you write the year in two digits in the Christian era, it seems that you will be scooped up in an unexpected place by conversion. Other than that, it seems to be relatively flexible. If you enter the creation date etc. in the file name with 4 digits in the Christian era, it will be easier to aggregate later. After that, datetime.today () is also received, so it may be good for logging.

If you want to convert explicitly, you can add the format option. For details, please see the official document below. https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html

Recommended Posts

I tried various patterns of date strings to be entered in pandas.to_datetime
I tried to make an analysis base of 5 patterns in 3 years
I tried to implement blackjack of card game in Python
I tried to implement PLSA in Python
I tried to implement permutation in Python
I tried to implement ADALINE in Python
I tried to implement PPO in Python
I tried to display the altitude value of DTM in a graph
I tried to implement a card game of playing cards in Python
I tried to put PyCharm in Ubuntu 16.04 LTS (PPA cannot be used)
I tried to open the latest data of the Excel file managed by date in the folder with Python
I tried to summarize the settings for various databases of Django (MySQL, PostgreSQL)
I tried to notify slack of Redmine update
I tried to find 100 million digits of pi
I tried to predict the horses that will be in the top 3 with LightGBM
I tried to touch the API of ebay
I tried to correct the keystone of the image
I tried to implement TOPIC MODEL in Python
I tried to implement selection sort in python
I tried to implement what seems to be a Windows snipping tool in Python
I tried to predict the price of ETF
I tried to vectorize the lyrics of Hinatazaka46!
[Linux] I learned LPIC lv1 in 10 days and tried to understand the mechanism of Linux.
I tried to publish GraphQL API of COVID19 infected person situation in Hyogo prefecture.
I tried to extract various information of remote PC from Python by WMI Library
I tried to extract the text in the image file using Tesseract of the OCR engine
I tried to put HULFT IoT (Agent) in the gateway Rooster of Sun Electronics
[First data science ⑥] I tried to visualize the market price of restaurants in Tokyo
I tried to graph the packages installed in Python
I tried to extract features with SIFT of OpenCV
I tried to summarize the basic form of GPLVM
I tried to implement a pseudo pachislot in Python
I tried to implement Dragon Quest poker in Python
I tried to implement GA (genetic algorithm) in Python
I tried to visualize the spacha information of VTuber
I tried to summarize how to use pandas in python
I tried to erase the negative part of Meros
[Python] I tried to get Json of squid ring 2
Use date to x-axis of tsplot depicted in seaborn
I tried to implement automatic proof of sequence calculation
I want to manipulate strings in Kotlin like Python!
I tried to classify the voices of voice actors
I tried to summarize the string operations of Python
I tried to debug.
I tried to paste
I tried to publish GraphQL API of COVID 19 infected person situation in Hyogo prefecture. (Part 2)
I tried to create a Python script to get the value of a cell in Microsoft Excel
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
I tried to put HULFT IoT (Edge Streaming) in the gateway Rooster of Sun Electronics
[Natural language processing] I tried to visualize the remarks of each member in the Slack community
I tried to find the trend of the number of ships in Tokyo Bay from satellite images.
I tried to find the entropy of the image with python
I tried to get the location information of Odakyu Bus
I tried the accuracy of three Stirling's approximations in python
I tried to create API list.csv in Python from swagger.yaml
I tried to make various "dummy data" with Python faker
I tried to find the average of the sequence with TensorFlow
I tried various methods to send Japanese mail with Python
I tried to implement a one-dimensional cellular automaton in Python
Processing of python3 that seems to be usable in paiza
I tried two ways to combine multiple commits in Git