Arrow date in a pandas column - date

I am using arrow to get the dates of a single dataframe that has the following structure:
data=['2015', '2016','2017', '2108']
df= pd.DataFrame(data,columns=['time'])
I know that to get the date in arrow is with the following code:
arrow.get('2016')
Have tried to use this:
arrow.get(df['time'])
But it gives me this error: Cannot parse single argument of type <class 'pandas.core.series.Series'>.
How to tell arrow to use the column?
Thanks

Convert the entire series for access later
One option is to use pandas apply on the column. https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html
df.time = df.time.apply(lambda tm: arrow.get(tm))
Might be a way to do this with converters on load as well, depending on where you are loading from. csv docs for example, https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html
I also wonder why you are using arrow time versus pandas built in datetime type. Once again, depending on how you are loading this data, dtypes could be used to specify datetime.
Convert one value from the series
You need to choose one value instead of providing all values (i.e. the pd.Series)
arrow.get(df.time[1]) would convert 2016 in your example.

Related

Google Sheets Script parseCsv - How to avoid converting text to numbers, data and formulas

I'm using the following code to import a csv file:
var csvData = Utilities.parseCsv(csvFile);
It is working but with some specific numbers, like "2.02" it is converting the text to date.
If I use the File > Import Menu, I'm able to avoid this automatic conversion, but I was not able to set this condition as a parameter on this function above (parseCSV).
Is it possible to use parseCSV and avoid this automatic text to date conversion?
Thank you!

Changing dd/mm/yyyy/ hh/mm/ss format to yyyymm in Hive

I'm using Hive at the moment. I have a column (column A) of strings which is in the following format 11/9/2009 0:00:00. I'd like to extract the yyyymm. i.e. I'd like the above string to be 200909. I've tried two different methods none of them worked.
I have tried to convert the string using two different methods
concat(year(Column A),lpad(month(Column A),2,0))
convert(datetime, Column A)
For the first row of code I'm receiving : NULL in all rows
For the second one I'm receiving :
Encountered: DATETIME Expected: ALL, CASE, CAST, DEFAULT, DISTINCT,
EXISTS, FALSE, IF, INTERVAL, NOT, NULL, REPLACE, TRUNCATE, TRUE,
IDENTIFIER CAUSED BY: Exception: Syntax error
Use unix_timestamp(string date, string pattern) to convert given date format to seconds passed from 1970-01-01. Then use from_unixtime() to convert to required format:
select from_unixtime(unix_timestamp( '11/9/2009 0:00:00','dd/MM/yyyy HH:mm:ss'), 'yyyyMM');
Result:
200909
Read also: Impala data and time functions and Hive date functions.
One more solution, works in Hive:
select concat(regexp_extract('11/9/2009 0:00:00','(\\d{1,2})/(\\d{1,2})/(\\d{4})',3),lpad(regexp_extract('11/9/2009 0:00:00','(\\d{1,2})/(\\d{1,2})/(\\d{4})',2),2,0))
Since I'm trying to turn strings into YYYYMM I have to use the below, which worked for me:
'concat(substr(Column A, instr(Column A, ' ')-4, 4),substr(Column A, instr(Column A, ' /')+1, 2))'

importing a text file in Access?

I have a large text file containing 4 attributes, I want to import this file in Microsoft access 2013 , but when the data is imported it gives an error
Type Conversion Failure
This error is occurring on Date Time field
The format of Date and Time is like that:
2008-02-02 15:36:08
Here is the sample of the file:
Sample of data
I guess you might have skipped explicitly defining the date format during the import procedure.
During the import process you need to click on advanced. This menu should appear:
you want to make sure that the highlighted fields match the date format in the text file.
EDIT: you will also want to make sure that the field is imported as Data Type Date Time

GEE Feature Collections and Dates

I have a fusiontable based feature collection in Google-Earth-Engine that includes a date column. I would like to cast the FC into an empty image and display with graduated colours for increasing dates - so that when the Inspector is used a human readable date is displayed.
var empty64 = ee.Image().toInt64();
var outlines = empty64.paint({
featureCollection: SomeFeatureCollection,
color: 'StartDate',
});
If I add this to the map as a layer I get the date as a 13-digit format that I can't read. How can I change this?
Thank you!
Per the API reference for Image.paint, color must be an object name or a number. To me, that means the EE API will interpret the object as a number, meaning a Date object will be converted from a string to a numerical representation. In this case, that's the "milliseconds since epoch" format.
Without a way to add metadata to a FeatureCollection (i.e. so you could store the associated datestring along with the other parameters of the feature), I don't think you can show a human readable date in the inspector.

Export Calendar Date to spreadsheetout - Time Stripped off - Google Script

I am using Google Script to export some calendar events to a spreadsheet; the relevant portion of my script is below:
var details=[[mycal,events[i].getTitle(), events[i].getDescription(), events[i].getLocation(), events[i].getStartTime(), myformula_placeholder, ('')]];
var range=sheet.getRange(row,1,1,7);
range.setValues(details);
This code works but the "time" that is put into the spreadsheet is a real number of the form nnnnn.nn. On the spreadsheet itself the date looks great using the integer to the left of the decimal (eg 10/15/2017) but the decimals are part of the value and therefore are part of the spreadsheet value.
My script drops the data into a sheet in my workbook, and another sheet reads the rows of data with the above date types, looking for specific date info from the other sheet using the match function (for today()). That would work fine if I could get rid of the decimals.
How can I use what I have above (if I stray far from what I have found works I will be redoing hours of work) but adding just what is needed to only put into the output spreadsheet the whole number portion so I have a pure date that will be found nicely by my match function using today()?
I have been digging, but errors abound in trying to put it all together. "Parse" looked like a good hope, but it failed as the validation did not like parse used within getStartTime. Maybe I used it in the wrong manner.
Help would be appreciated greatly.
According to the CalendarApp documentation, getStartTime() generates a Date object. You should be able to extract the date and time separately from the date object:
var eventStart = events[i].getStartTime(); // Returns date object
var startDate = eventStart.toDateString(); // Returns date portion as a string
var startTime = eventStart.toTimeString(); // Returns time portion as a string
You could then write one or both of these to your spreadsheet. See the w3schools Javascript Date Reference for more information:
http://www.w3schools.com/jsref/jsref_obj_date.asp
If you If you want to specify the string format, you can try formatDate in the Utilities service:
https://developers.google.com/apps-script/reference/utilities/utilities#formatdatedate-timezone-format
You could just use the Math.floor() function
http://www.w3schools.com/jsref/jsref_floor.asp
which will round the real number to an integer. Your line would then read:
var details=[[mycal,events[i].getTitle(), events[i].getDescription(), events[i].getLocation(), Math.floor(events[i].getStartTime()), myformula_placeholder, ('')]];