Python/Pandas: How do I convert from datetime64[ns] to datetime

I have a script that processes an Excel file. The department that sends it has a system that generated it, and my script stopped working.

I suddenly got the error Can only use .str accessor with string values, which use np.object_ dtype in pandas for the following line of code:

df['DATE'] = df['Date'].str.replace(r'[^a-zA-Z0-9\._/-]', '')

I checked the type of the date columns in the file from the old system (dtype: object) vs the file from the new system (dtype: datetime64[ns]).

How do I change the date format to something my script will understand?

I saw this answer but my knowledge about date formats isn't this granular.

You can use pd.to_datetime

df['DATE'] = pd.to_datetime(df['DATE'])

pandas.to_datetime — pandas 1.1.1 documentation, list-like: DatetimeIndex. Series: Series of datetime64 dtype. scalar: Timestamp. In case when it is not possible to return designated types (� How do I convert a numpy.datetime64 object to a datetime.datetime (or Timestamp)? In the following code, I create a datetime, timestamp and datetime64 objects. import datetime import numpy as np import pandas as pd dt = datetime . datetime ( 2012 , 5 , 1 ) # A strange way to extract a Timestamp object, there's surely a better way? ts = pd .

You can use apply function on the dataframe column to convert the necessary column to String. For example:

df['DATE'] = df['Date'].apply(lambda x: x.strftime('%Y-%m-%d'))

Make sure to import datetime module.

apply() will take each cell at a time for evaluation and apply the formatting as specified in the lambda function.

Time Series / Date functionality — pandas 0.23.0 documentation, In working with time series data, we will frequently seek to: to 45 minute frequency and forward fill In [5]: converted = ts.asfreq('45Min', method='pad') In Out[25]: DatetimeIndex(['2012-01-04 10:00:00'], dtype='datetime64[ns]', freq= None) In� How do I convert a numpy.datetime64 object to a datetime.datetime (or Timestamp)? In the following code, I create a datetime, timestamp and datetime64 objects. import datetime . import numpy as np . import pandas as pd . dt = datetime.datetime(2012, 5, 1) # A strange way to extract a Timestamp object, there's surely a better way?

pd.to_datetime returns a Series of datetime64 dtype, as described here:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html

df['DATE'] = df['Date'].dt.date

or this:

df['Date'].map(datetime.datetime.date) 

Converting between datetime, Timestamp and datetime64, To convert between datetime, timestamp and datetime64 you can use the below- mentioned code:- >>> from datetime import datetime. Convert argument to datetime. Parameters arg int, float, str, datetime, list, tuple, 1-d array, Series, DataFrame/dict-like. The object to convert to a datetime. errors {‘ignore’, ‘raise’, ‘coerce’}, default ‘raise’ If ‘raise’, then invalid parsing will raise an exception. If ‘coerce’, then invalid parsing will be set as

[SOLVED] How to convert numpy datetime64 into datetime, Borrowing from Converting between datetime, Timestamp and datetime64. In [ 220]: x Out[220]: numpy.datetime64('2012-06-17T23:00:05.453000000-0700') In � How do I convert a numpy.datetime64 object to a datetime.datetime (or Timestamp)?. In the following code, I create a datetime, timestamp and datetime64 objects. import datetime import numpy as np import pandas as pd dt = datetime.datetime(2012, 5, 1) # A strange way to extract a Timestamp object, there's surely a better way? ts = pd.DatetimeIndex([dt])[0] dt64 = np.datetime64(dt) In [7]: dt

Pandas Datetime: Manipulate and convert date times with timezone , Pandas Datetime, Practice and Solution: Write a Pandas program to Python Code : '2018-01-01 02:00:00+00:00'], dtype='datetime64[ns, UTC]', freq='H') From UTC to check_type(5) do something with an int do something with an int or float >>> check_type(4.2) do something with an int or float� 2018-10-14T11:33:53+05:30 2018-10-14T11:33:53+05:30 Amit Arora Amit Arora Python Programming Tutorial Python Practical Solution Share on Facebook Share on Twitter

Converting between datetime, Timestamp and datetime64, Python/Pandas: How do I convert from datetime64[ns] to datetime , argint, float, str, datetime, list, tuple, 1-d array, Series DataFrame/dict-like 5]})� Although pd.to_datetime could do its job without giving the format smartly, the conversion speed is much lower than that when the format is given.. We could set the option infer_datetime_format of to_datetime to be True to switch the conversion to a faster mode if the format of the datetime string could be inferred without giving the format string.

Comments
  • You can convert dates back to strings with df['Date'].dt.strftime('%Y-%m-%d').
  • That did it, thanks.